JS Timestamp converted to date format

Source: Internet
Author: User

Original: JS timestamp converted to date format

JS Timestamp converted to date format

What is a Unix timestamp (Unix timestamp): Unix timestamp (Unix timestamp), or Unix time (Unix times), POSIX time, is a time representation, Defined as the total number of seconds from GMT January 01, 1970 00:00 00 seconds to now. Unix timestamps are used not only in UNIX systems, Unix-like systems, but also in many other operating systems.

A significant portion of the operating system is currently using 32-bit binary numbers to represent time. UNIX timestamps of such systems can be used up to Greenwich Mean time of January 19, 2038 03:14 07 seconds (binary: 01111111 11111111 11111111 11111111). After one second, the binary number becomes 10000000 00000000 00000000 00000000, and an overflow error occurs, causing the system to misinterpret the time as December 13, 1901 20:45 52 seconds. This is likely to cause a software failure, or even a system crash. Systems that use 64-bit binary numbers to represent time (up to GMT 292,277,026,596, December 04, 15:30, 08 seconds) do not experience this kind of overflow.

A. js converts the time into a timestamp

1.js how to get the current timestamp

var timestamp1 = Date.parse (new Date ());
var timestamp2 = (new Date ()). ValueOf ();
var New Date (). GetTime ();

The first: The timestamp is obtained by changing the millisecond to 000, and the second and third is to obtain the timestamp of the current millisecond.

2.js get the method for making timestamps

var oldtime = (new Date ("2015/06/23 08:00:20")). GetTime ()/1000;

GetTime () returns the number of milliseconds in the unit.

Demonstrate

Two. js converts the timestamp to the normal date format

1.Date toLocaleString Method

function Getlocaltime (NS) {        returnnew Date (parseint (NS) *). toLocaleString (). Replace (/:\ d{1,2}$/, ');     }

The parseint () function parses a string and returns an integer.

The time operation unit in JS is milliseconds.

The toLocaleString () method converts a Date object to a string based on local time and returns the result.

The replace () method is used to replace other characters with some characters in a string, or to replace a substring that matches a regular expression.

Replace (/:\d{1,2}$/, ') to verify replacement: Start with an end string of one or two digits, which is the second; Replace with empty

Shown below:

Demonstrate

So we can use regular expressions to change the date format we want.

2.Date Attribute method

functionAdd0 (m) {returnM<10? ' 0 ' +M:m}functionFormat (Shijianchuo) {//Shijianchuo is an integer, otherwise parseint conversionvarTime =NewDate (Shijianchuo);vary =time.getfullyear ();varm = Time.getmonth () +1;varD =time.getdate ();varh =time.gethours ();varMM =time.getminutes ();vars =time.getseconds ();returny+ '-' +add0 (m) + '-' +add0 (d) + ' +add0 (h) + ': ' +add0 (mm) + ': ' +add0 (s);}

Demonstrate

Three. Encapsulated Time Formatter
/** * php-like timestamp format function * @param {string} format format * @param {int} timestamp the time to format is implicitly considered the current time * @return {string The formatted time string*/functionDate (format, timestamp) {varA, jsdate= (timestamp)?NewDate (timestamp*1000):NewDate ()); varPad =function(n, c) {if(n = n + ""). Length <c) {             return NewArray (++c-n.length). Join ("0") +N; } Else {             returnN;     }     }; varTxt_weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; varTxt_ordin = {1: "St", 2: "nd", 3: "rd", +: "St", "nd", at: "Rd", +: "St"}; varTxt_months = ["", "January", "February", "March", "April", "may", "June", "July", "August", "September", "October", "Novem ber "," December "]; varf = {         // DayD:function(){returnPad (F.J (), 2)}, D:function(){returnF.L (). substr (0,3)}, J:function(){returnjsdate.getdate ()}, L:function(){returnTXT_WEEKDAYS[F.W ()]}, N:function(){returnF.W () + 1}, S:function(){returnTXT_ORDIN[F.J ()]? TXT_ORDIN[F.J ()]: ' th '}, W:function(){returnjsdate.getday ()}, Z:function(){return(Jsdate-NewDate (Jsdate.getfullyear () + "/1/1"))/864e5 >> 0},                 //WeekW:function(){             varA = F.z (), B = 364 + F.L ()-A; varNd2, ND = (NewDate (Jsdate.getfullyear () + "/1/1"). GetDay () | | 7)-1; if(b <= 2 && ((Jsdate.getday () | | 7)-1) <= 2-b) {                 return1; } Else{                 if(A <= 2 && nd >= 4 && a >= (6-nd)) {Nd2=NewDate (Jsdate.getfullyear ()-1 + "/12/31"); returnDate ("W", Math.Round (Nd2.gettime ()/1000)); }Else{                     return(1 + (nd <= 3?) ((A + nd)/7): (A-(7-ND))/7) >> 0); }             }         },                 //MonthF:function(){returnTXT_MONTHS[F.N ()]}, M:function(){returnPad (F.N (), 2)}, M:function(){returnF.F (). substr (0,3)}, N:function(){returnJsdate.getmonth () + 1}, T:function(){             varN; if((n = jsdate.getmonth () + 1) = = 2 ){                 return28 +F.L (); } Else{                 if(N & 1 && N < 8 | |! (N & 1) && n > 7 ){                     return31; } Else{                     return30; }             }         },                 // YearLfunction(){vary = F.y ();return(! (Y & 3) && (y% 1e2 | |! ( Y% 4e2))? 1:0},         //o not supported yetY:function(){returnjsdate.getfullyear ()}, Y:function(){return(Jsdate.getfullyear () + ""). Slice (2)},                 // TimeA:function(){returnJsdate.gethours () > 11? "PM": "AM"}, A:function(){returnF.A (). toUpperCase ()}, B:function(){             //Peter Paul Koch:            varOff = (Jsdate.gettimezoneoffset () + 60) *60; varTheseconds = (jsdate.gethours () * 3600) + (Jsdate.getminutes () * +) + jsdate.getseconds () +off; varBeat = Math.floor (theseconds/86.4);if(Beat > Beat)-= 1000; if(Beat < 0) beat + = 1000; if(String (Beat)). length = = 1) beat = "00" +beat; if(String (Beat)). length = = 2) beat = "0" +beat; returnbeat; }, G:function(){returnJsdate.gethours ()% 12 | | 12}, G:function(){returnjsdate.gethours ()}, H:function(){returnPad (F.G (), 2)}, H:function(){returnPad (Jsdate.gethours (), 2)}, I:function(){returnPad (Jsdate.getminutes (), 2)}, S:function(){returnPad (Jsdate.getseconds (), 2)},         //u not supported yet                //Timezone        //e not supported yet        //I not supported yetO:function(){             vart = Pad (Math.Abs (Jsdate.gettimezoneoffset ()/60*100), 4);if(Jsdate.gettimezoneoffset () > 0) t = "-" + t;Elset = "+" +T; returnT; }, P:function(){varO = F.O ();return(O.substr (0, 3) + ":" + o.substr (3, 2))},         //T not supported yet        //Z not supported yet                //Full date/timeC:function(){returnF.Y () + "-" + f.m () + "-" + f.d () + "T" + f.h () + ":" + f.i () + ":" + f.s () +f.p ()},//R not supported yetUfunction(){returnMath.Round (Jsdate.gettime ()/1000)}    }; returnFormat.replace (/[\]? ( [A-za-z]) /g,function(t, s) {if(t!=s) {             //escapedRET =s; } Else if(F[s]) {//A date function existsRET =F[s] (); } Else{             //Nothing SpecialRET =s; }         returnret; }); }

The calling method makes it easy to convert timestamps into date formats, such as:

Date (' y-m-d ', ' 1350052653 '); //  Date (' y-m-d h:i:s ', ' 1350052653 '); // The result is 2012-10-12 22:37:33

--------------------------------------------------------------------------------------------------------------- ----------------------

Finish

Reproduced must be reproduced in the words, the original author and the original post address.

JS Timestamp converted to date format

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.