JavaScript timestamps and date strings are converted to each other

Source: Internet
Author: User

stolen from: http://www.cnblogs.com/yjf512/p/3796229.html
<HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><Scripttype= "Text/javascript">//Gets the current timestamp (in s)vartimestamp=Date.parse (NewDate ()); timestamp=timestamp/ +;//the current timestamp is: 1403149534Console.log ("the current timestamp is:" +timestamp);//gets the timestamp of a time formatvarStringTime= "2014-07-10 10:21:12";varTIMESTAMP2=Date.parse (NewDate (StringTime)); TIMESTAMP2=TIMESTAMP2/ +;//the timestamp for 2014-07-10 10:21:12 is: 1404958872Console.log (StringTime+ "the timestamp is:" +timestamp2);//change the current time to a time format stringvarTimestamp3= 1403058804;varnewdate= NewDate (); Newdate.settime (Timestamp3* +);//Wed JuneConsole.log (newdate.todatestring ());//Wed, June 02:33:24 GMTConsole.log (newdate.togmtstring ());//2014-06-18t02:33:24.000zConsole.log (newdate.toisostring ());//2014-06-18t02:33:24.000zConsole.log (Newdate.tojson ());//June 18, 2014Console.log (newdate.tolocaledatestring ());//June 18, 2014 morning 10:33:24Console.log (newdate.tolocalestring ());//Morning 10:33:24Console.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 GMTConsole.log (newdate.toutcstring ());D Ate.prototype.format= function(format) {varDate= { "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 (varkinchdate) { if (NewRegExp ("(" +k+ ")"). Test (format)) {format=Format.replace (regexp.$1, regexp.$1. Length== 1 ?Date[k]: ("xx" +date[k]). substr ("" +Date[k]) (length)); } } returnformat;} Console.log (Newdate.format ('YYYY-MM-DD h:m:s'));</Script>

JavaScript timestamps and date strings are converted to each other

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.