JavaScript timestamps and date strings are converted to each other

Source: Internet
Author: User

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<script type= "Text/javascript" >
Gets the current timestamp (in s)
var timestamp = date.parse (new Date ());
timestamp = timestamp/1000;
The current timestamp is: 1403149534
Console.log ("Current timestamp is:" + timestamp);

Gets the timestamp of a time format
var stringtime = "2014-07-10 10:21:12";
var timestamp2 = date.parse (new Date (StringTime));
TIMESTAMP2 = timestamp2/1000;
2014-07-10 10:21:12 Timestamp: 1404958872
Console.log (StringTime + "timestamp:" + timestamp2);

Change the current time to a time format string
var Timestamp3 = 1403058804;
var newdate = new Date ();
Newdate.settime (Timestamp3 * 1000);
Wed June 18 2014
Console.log (Newdate.todatestring ());
Wed, June 02:33:24 GMT
Console.log (Newdate.togmtstring ());
2014-06-18t02:33:24.000z
Console.log (Newdate.toisostring ());
2014-06-18t02:33:24.000z
Console.log (Newdate.tojson ());
June 18, 2014
Console.log (Newdate.tolocaledatestring ());
June 18, 2014 10:33:24
Console.log (Newdate.tolocalestring ());
10:33:24
Console.log (Newdate.tolocaletimestring ());
Wed June 10:33:24 gmt+0800 (China Standard Time)
Console.log (Newdate.tostring ());
10:33:24 gmt+0800 (China Standard Time)
Console.log (Newdate.totimestring ());
Wed, June 02:33:24 GMT
Console.log (Newdate.toutcstring ());

Date.prototype.format = function (format) {
var date = {
"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+)/i.test (format)) {
Format = Format.replace (regexp.$1, (this.getfullyear () + "). substr (4-regexp.$1.length));
}
For (var k in date) {
if (New RegExp ("(" + K + ")"). Test (format)) {
Format = Format.replace (regexp.$1, regexp.$1.length = = 1
? Date[k]: ("XX" + date[k]). substr (("" + date[k]). length);
}
}
return format;
}
Console.log (Newdate.format (' Yyyy-mm-dd h:m:s '));

</script>

JavaScript timestamps and date strings are converted to each other

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.