admin管理员组

文章数量:1287078

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

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

Share Improve this question edited May 20, 2020 at 8:24 luke77 3,7033 gold badges22 silver badges31 bronze badges asked Feb 18, 2012 at 18:34 user1202278user1202278 7732 gold badges10 silver badges24 bronze badges 1
  • As for formatting to a final string I could suggest you the following library. As far as the first part of the question is concerned you could refer to the following question – Oybek Commented Feb 18, 2012 at 18:41
Add a comment  | 

6 Answers 6

Reset to default 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/

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;

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

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

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.

try this blog it has enough dateformats:

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

本文标签: Date format conversion in JavaScriptStack Overflow