Javascript-js How to convert seconds into dates?

Source: Internet
Author: User
Now there is a situation: back in the background to give me a January 1, 1970 to the current number of seconds, and then I need to convert the number of seconds to years and days, I think of some methods are considered to be too complex trouble, want to ask you have any good points of the method or ideas?

Reply content:

Now there is a situation: back in the background to give me a January 1, 1970 to the current number of seconds, and then I need to convert the number of seconds to years and days, I think of some methods are considered to be too complex trouble, want to ask you have any good points of the method or ideas?

Direct code

Date.prototype.Format = function (fmt) {     var o = {        "M+": this.getMonth() + 1, //月份         "d+": this.getDate(), //日         "h+": this.getHours(), //小时         "m+": this.getMinutes(), //分         "s+": this.getSeconds(), //秒         "q+": Math.floor((this.getMonth() + 3) / 3), //季度         "S": this.getMilliseconds() //毫秒     };    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));    for (var k in o)    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));    return fmt;}//调用new Date(time).Format("yyyy-MM-dd hh:mm");

var d = new Date(1463068800 * 1000);alert(d.toISOString());

1463068800That is, unix time stamps. The above method can be converted directly to the js time in.

var d = new Date ()
D.settime (seconds * 1000);
var year = D.getfullyear ();
Var...
You can get everything you want, date and year.

Use Moment.js directly, easy to understand http://momentjs.cn/

Using Moment.js

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.