10

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

2012-02-18 14:28:32

to a date presentation of this format -

Saturday Feb 2012 14:28:32

Many thanks :)

luke77
  • 2,255
  • 2
  • 18
  • 30
user1202278
  • 774
  • 2
  • 10
  • 24
  • As for formatting to a final string I could suggest you [the following library](http://blog.stevenlevithan.com/archives/date-time-format). As far as the first part of the question is concerned you could refer to the [following question](http://stackoverflow.com/questions/9229213/convert-iso-date-to-milliseconds-in-javascript/9229384) – Oybek Feb 18 '12 at 18:41

6 Answers6

5

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 was me, i would look into Datejs: http://www.datejs.com/ best javascript date api for me

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

jribeiro
  • 3,387
  • 8
  • 43
  • 70
3

You must first define an array of the English words (Sunday, Monday, Feb, Mar, etc.):

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

To be able insert the extra 0 at the beginning of the minutes and seconds, define a padding 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 like this:

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 the whole thing by concatenating formattedDate and formattedTime, as in:

wholeThing = formattedDate + " " + formattedTime;
wecsam
  • 2,651
  • 4
  • 25
  • 46
3

Consider using datejs which is rocks!

var mydate = Date.parse('2012-02-18 14:28:32');
var result = mydate.toString('dddd MMM yyyy h:mm:ss');
console.log(result);
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Juicy Scripter
  • 25,778
  • 6
  • 72
  • 93
  • 2
    ??? Uncaught RangeError: toString() radix argument must be between 2 and 36 at Number.toString () at :2:21 – Paolo Biavati Mar 13 '17 at 15:21
  • @PaoloBiavati that means you call `toString` on a number, not a Date: https://stackoverflow.com/questions/16527109/javascript-date-tostring-radix-argument-error (use datejs). – Ehvince Jul 28 '17 at 17:53
2

I'd suggest using an external js library to do that. To my understanding, Moment.js is the best date-time conversion library out there.

In this case, it does the job in one line. Just add the moment.js in you 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
onezero
  • 109
  • 1
  • 10
1

JavaScripts Date object is lacking methods for formatting. I would consider using an external library like this one. Seems it has what you're looking for.

Björn
  • 29,019
  • 9
  • 65
  • 81
0

try this blog it has enough dateformats:

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

animuson
  • 53,861
  • 28
  • 137
  • 147
Vikram
  • 8,235
  • 33
  • 47