The method of converting JS timestamp to date format _javascript tips

Source: Internet
Author: User
Tags local time posix timestamp to date

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

A considerable portion of the operating system currently uses 32-bit binary digits to represent time. Unix timestamps of this type of system can be used up to GMT January 19, 2038 03:14 07 seconds (binary: 01111111 11111111 11111111 11111111). A second later, 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 paralysis. Systems that use 64-bit binary digits to represent time (up to GMT 292,277,026,596, December 04 15:30 08 seconds) are less likely to experience such overflow problems.

A. js converts time to time stamp
1.js method to get current timestamp

var timestamp1 = date.parse (New Date ());
var timestamp2 = (new Date ()). valueof ();
var Timestamp3 = new Date (). GetTime ();

First: The timestamp obtained is to change the milliseconds to 000 display, and the second and third is to get the timestamp of the current millisecond.

2.js get the method to make time stamp

var oldtime = (New Date ("2015/06/23 08:00:20"). GetTime ()/1000;
GetTime () returns the number of milliseconds in the unit.

Two. js converts the timestamp to the normal date format
1.Date tolocalestring Method

function Getlocaltime (NS) {return  
 new Date (parseint (NS) * 1000). 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 the Date object to a string based on local time and returns the result.

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

Replace (/:\d{1,2}$/, ') Verify replacement with: Start with one or two digits of the end string, that is, seconds; Replace with null

The display is as follows:

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

2.Date Property Method

function add0 (m) {return m<10? ' 0 ' +m:m}
function Format (Shijianchuo)
{
//shijianchuo is an integer, otherwise you will parseint convert
var time = new Date ( Shijianchuo);
var y = time.getfullyear ();
var m = time.getmonth () +1;
var d = time.getdate ();
var h = time.gethours ();
var mm = Time.getminutes ();
var s = time.getseconds ();
Return y+ '-' +add0 (m) + '-' +add0 (d) + ' +add0 (h) + ': ' +add0 (mm) + ': ' +add0 (s);
}

Three. The encapsulated time format device

/** * and PHP timestamp Format function * @param {string} format * @param {int} timestamp the time to format the current time * @return {string} 
 Typed time string/function date (format, timestamp) {var A, jsdate= (timestamp)? New Date (timestamp*1000): new Date ()); 
  var pad = function (n, c) {if ((n = n + ""). Length < C) {return new Array (++c-n.length). Join ("0") + N; 
  else {return n; 
 } 
 }; 
 var txt_weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; 
 var Txt_ordin = {1: "St", 2: "nd", 3: "Rd", "St.", "nd", "rd", "St"}; var txt_months = ["", "January", "February", "March", "April", "may", "June", "July", "August", "September", "October", "N 
 Ovember "," December "]; var f = {//day d:function () {return pad (F.J (), 2)}, D:function () {return F.L (). substr (0,3)}, J:function () {R Eturn jsdate.getdate ()}, L:function () {return TXT_WEEKDAYS[F.W ()]}, N:function () {return F.W () + 1}, S:function ( ) {return TXT_ORDIN[F.J ()]? txt_orDIN[F.J ()]: ' th '}, W:function () {return jsdate.getday ()}, Z:function () {return (jsdate-new 
   R () + "/1/1"))/864e5 >> 0},//Week w:function () {var a = F.z (), B = 364 + F.L ()-A; 
   var nd2, nd = (new Date (Jsdate.getfullyear () + "/1/1"). Getday () | | 7)-1; 
   if (b <= 2 && (jsdate.getday () | | | 7)-1) <= 2-b) {return 1; } else{if (a <= 2 && nd >= 4 && a >= (6-nd)) {Nd2 = new Date (Jsdate.getfullyear ()- 
     1 + "/12/31"); 
    Return date ("W", Math.Round (Nd2.gettime ()/1000)); else{return (1 + (nd <= 3?) 
    ((A + nd)/7: (A-(7-ND))/7) >> 0); }},//Month f:function () {return TXT_MONTHS[F.N ()]}, M:function () {return pad (F.N (), 2)}, M:fun 
   Ction () {return f.f (). substr (0,3)}, N:function () {return jsdate.getmonth () + 1}, T:function () {var n; 
   if ((n = jsdate.getmonth () + 1) = = 2) {return + F.L (); }else{if (n & 1 && N < 8 | |! 
    N & 1) && n > 7) {return 31; 
    } else{return 30; }},//year l:function () {var y = f.y (); Y & 3) && (y% 1e2 | |!) ( Y% 4e2))? 1:0},//o not supported yet y:function () {return jsdate.getfullyear ()}, Y:function () {return (Jsdate.getfullyea R () + ""). Slice (2)},//Time a:function () {return jsdate.gethours () > 11? PM ': ' AM '}, A:function () {return F.A (). toUpperCase ()}, B:function () {//Peter Paul Koch:var off = (jsdate 
   . getTimezoneOffset () + 60) *60; 
   var theseconds = (jsdate.gethours () * 3600) + (Jsdate.getminutes () *) + jsdate.getseconds () + off; 
   var beat = Math.floor (theseconds/86.4); 
   if (Beat > 1000) beat-= 1000; 
   if (Beat < 0) beat + + 1000; 
   if ((String (Beat)). length = = 1) beat = "a" +beat; 
   if ((String (Beat)). length = = 2) beat = "0" +beat; 
  return beat; }, G:function () {return jsdate. GetHours ()% 12 | | (), G:function () {return jsdate.gethours ()}, H:function () {return pad (F.G (), 2)}, H:function () {return pad (jsdat E.gethours (), 2)}, I:function () {return pad (jsdate.getminutes (), 2)}, S:function () {return pad (jsdate.getseconds), 2)},//u not supported yet//Timezone//e not supported yet//i not supported yet () {VA 
   R T = Pad (Math.Abs (Jsdate.gettimezoneoffset ()/60*100), 4); if (Jsdate.gettimezoneoffset () > 0) t = "-" + t; 
   else T = "+" + t; 
  return t; }, P:function () {var O = F.O (), Return (O.SUBSTR (0, 3) + ":" + o.substr (3, 2))},//t is not supported yet the not SUP  Ported yet//full date/time c:function () {return f.y () + "-" + f.m () + "-" + f.d () + "T" + f.h () + ":" + f.i () + 
  
 ":" + f.s () + F.P ()},//r not supported yet u:function () {return Math.Round (Jsdate.gettime ()/1000)}}; Return Format.replace (/[\]? ( [A-za-z]) 
  /g, function (t, s) {if (t!=s) {//escaped RET = s;else if (F[s]) {//A date function exists ret = f[s] (); 
  } else{//Nothing special RET = s; 
 return ret; 
});
 }

Calling a method makes it easy to convert a timestamp into a date format, such as:

Date (' y-m-d ', ' 1350052653 '); it is convenient to convert the timestamp to 2012-10-11 
date (' y-m-d h:i:s ', ' 1350052653 '); the result is 2012-10-12. 22:37:33

The above is the JS timestamp to date format method, I hope to help you learn.

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.