JavaScript timestamp conversion

I am wondering what is the best way to convert the timestamp of this format -

2012-02-18 14:28:32 

by the date of submission of this format -

 Saturday Feb 2012 14:28:32 

Thank you very much:)

+9
javascript date datetime timestamp
Feb 18 '12 at 18:34
source share
6 answers

Javascript date functions are pretty bad ... You have the option to convert to UTC http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_toutcstring

But if it were me, I would look at Datejs: http://www.datejs.com/ the best javascript date api for me

Please take a look at getting started with Datejs: http://www.datejs.com/2007/11/27/getting-started-with-datejs/

+5
Feb 18 '12 at 18:40
source share

Consider using datejs , which is a stone!

 var mydate = Date.parse('2012-02-18 14:28:32'); var result = mydate.toString('dddd MMM yyyy h:mm:ss'); console.log(result); 
+3
Feb 18 '12 at 18:45
source share

First you must define an array of English words (Sunday, Monday, February, March, etc.):

 var daysOfWeek = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"], monthsOfYear = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]; 

To insert an extra 0 at the beginning of minutes and seconds, define a fill function for the String prototype:

 String.prototype.padLeft = function(padString,length){ var toReturn = String(this); while(toReturn.length < length){ toReturn = padString + toReturn; } return toReturn; } 

Format the date and time as follows:

 var time = new Date(), formattedDate, formattedTime, wholeThing; formattedDate = daysOfWeek[time.getDay()] + ", " + monthsOfYear[time.getMonth()] + " " + time.getDate() + ", " + time.getFullYear(); formattedTime = time.getHours() + ":" + time.getMinutes().padLeft("0",2) + time.getSeconds().padLeft("0",2); 

You can get all this by combining formattedDate and formattedTime , as in:

 wholeThing = formattedDate + " " + formattedTime; 
+2
Feb 18 '12 at 18:43
source share

The JavaScripts Date object lacks formatting methods. I would consider using an external library such as this one . It seems to have what you are looking for.

+1
Feb 18 '12 at 18:39
source share

try this blog, it has enough data feed:

http://blog.stevenlevithan.com/archives/date-time-format

+1
Feb 18 '12 at 18:43
source share

I would suggest using an external js library for this. As far as I understand, Moment.js is the best library for converting date and time.

In this case, he performs the task on one line. Just add moment.js to the project and then do

 var timestamp = '2012-02-18 14:28:32'; var formattedTime = moment(timestamp).format('dddd MMM YYYY HH:mm:ss'); // Saturday Feb 2012 14:28:32 
+1
Jun 25 '17 at 2:31 on
source share



All Articles