1

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

1
  • 1
    you can use date = new Date(1469167371657) Commented Jul 22, 2016 at 9:24

5 Answers 5

2

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));

Sign up to request clarification or add additional context in comments.

Comments

1

with pure js you can do the folowing

var d = new Date(); console.log(d.getMonth() + 1 + "/" + d.getDate() + "/" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes()) 

Comments

0

You can use - http://momentjs.com/ and have it done like:

moment(1469167371657).format('MM/DD/YYYY HH:MM') 

Comments

0

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

Comments

0

I believe that number is milliseconds so to convert it to date, you would do this:

var time = new Date().getTime(); var date = new Date(time); alert(date.toString()); // Wed Jan 12 2011 12:42:46 GMT-0800 (PST) 

var time=1469167371657; var date = new Date(time); alert(date.toString()); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.