The conversion between JS timestamp and datetime

Source: Internet
Author: User

1. DateTime Conversion to timestamp
Strdate = "2015-08-09 08:01:36:789var d = new Date (strdate); var timestamp=Math.Round(d.GetTime());d ocument.write ("Timestamp:" +timestamp+ "<br>"); output:Timestamp:1439078496789
Other methods:var timestamp = Date. Parse (strdate);orvar timestamp = d.valueOf();Note:strdate to add milliseconds, or convert to DateTime will miss milliseconds.
2. Timestamp converted to datetime

functiontimestamp2string(time)         {var datetime = new Date ();         Datetime.settime (time);         var year = Datetime.getfullyear ();         var month = Datetime.getmonth () + 1;         var date = Datetime.getdate ();         var hour = datetime.gethours ();         var minute = Datetime.getminutes ();         var second = Datetime.getseconds ();         var mseconds = Datetime.getmilliseconds (); Return year + "-" + month + "-" + date+ "" +hour+ ":" +minute+ ":" +second+ "." +mseconds;}; var time = "1439078496789"; or time=1439018115000; As a result, var strdate2 = timestamp2string (time);d ocument.write ("Date:" +strdate2+ "<br>"), Output:Date:2015-8-9 8:1:36.789

Copyright NOTICE: This article for Bo Master original article, welcome reprint, reproduced please indicate the source.

The conversion between JS timestamp and datetime

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.