Skip to content Skip to sidebar Skip to footer

Javascript Date Time_ T Conversion

I'm having a problem with the date format in javascript. I'm getting a date in seconds (in time_t format) from a database (ex. 1364565600) Now I want to convert this date to day, m

Solution 1:

Multiply the seconds you're getting by 1000 and use a new Date() object, which takes milliseconds as a parameter (which is based on the same idea as time_t, which is seconds since epoch, but Date() is based on milliseconds):

 timestart: function (time_start) {

            return new Date(1000 * time_start);
         }

To get the Date string from it, use .toDateString(). There are a few other methods you could use to grab the date information and convert it to the types you want, you can find them here: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date


Solution 2:

For verbose date manipulation in JavaScript, it's easiest to use a library if your deployment/optimization requirements permit. I've found Moment JS to be a good solution.


Solution 3:

The time value in a javascript date object is milliseconds since 1970-01-01T00:00:00Z (note UTC). As Rob G said, you can pass that value to the Date constructor to get a date object, then use Date methods to get what you want.

If you have a UTC time value in seconds from the same epoch, multiply it by 1,000 to get milliseconds and pass to the date constructor:

var date = new Date(timeValue);

You can now get either local or UTC date and time strings from the object using methods like toString, toLocaleString, toISOString and so on. You can also use standard Date methods to get local time and date values, or UTC methods (e.g. getUTCFullYear) to get UTC values, to build a string of your own format.


Post a Comment for "Javascript Date Time_ T Conversion"