javascript date time_ t conversion

*爱你&永不变心* 提交于 2020-01-25 07:40:06

问题


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, month, day (ex. Tuesday, March, 18th).

I hope this is possible.

 timestart: function (time_start) {
                ///////////////////////////////
                //////code for conversion//////
                return  time_start;
             }

Thanks in advance!


回答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




回答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.




回答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.



来源:https://stackoverflow.com/questions/15674093/javascript-date-time-t-conversion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!