I have a date string which coming from the db as follows
/Date(1469167371657)/
Is there any way to convert this date to following format using javascript
MM/DD/YYYY HH:MM
I've searched a lot but unble to find a solution
In plain javascript you have to write your own function for string format a date, for example for your string format:
var date = new Date(1469167371657); function stringDate(date) { var mm = date.getMonth()+1; mm = (mm<10?"0"+mm:mm); var dd = date.getDate(); dd = (dd<10?"0"+dd:dd); var hh = date.getHours(); hh = (hh<10?"0"+hh:hh); var min = date.getMinutes(); min = (min<10?"0"+min:min); return mm+'/'+dd+'/'+date.getFullYear()+" "+hh+":"+min; } console.log(stringDate(date)); drier code version
var date = new Date(1469167371657); function stringDate(date) { return ("0" + (date.getMonth() + 1)).slice(-2)+'/' +("0" + date.getDate()).slice(-2)+'/' +date.getFullYear()+" " +("0" + date.getHours()).slice(-2)+':' +("0" + date.getMinutes()).slice(-2) } console.log(stringDate(date)); You can use - http://momentjs.com/ and have it done like:
moment(1469167371657).format('MM/DD/YYYY HH:MM') You can do this with the following steps:
1) convert the timestamp to a date object.
var timestamp = "/Date(1469167371657)/"; // However you want to save whatever comes from your database timestamp = timestamp.substr(timestamp.indexOf("(")+1); // gives 1469167371657)/ timestamp = timestamp.substr(0,timestamp.indexOf(")")); // gives 1469167371657 var d = new Date(timestamp); 2) set it to your format
function leadZero(i) {if(i < 10) {return "0"+i;} return i;} // Simple function to convert 5 to 05 e.g. var time = leadZero(d.getMonth()+1)+"/"+leadZero(d.getDate())+"/"+d.getFullYear()+" "+leadZero(d.getHours())+":"+leadZero(d.getMinutes()); alert(time); Note: the date / timestamp you provided is too high for javascript to understand, so this example will not work correclty
date = new Date(1469167371657)