JS Timestamp converted to date format

Source: Internet
Author: User
Tags current time getdate

Let's see the first one here.

The code is as follows Copy Code


<script>
function Getlocaltime (NS) {
return new Date (parseint (NS) * 1000). toLocaleString (). Replace (/:d {1,2}$/, ");
}
Alert (Getlocaltime (1293072805));
</script>

The result is
December 23, 2010 10:53

Second Kind

The code is as follows Copy Code

<script>
function Getlocaltime (NS) {
return new Date (parseint (NS) * 1000). toLocaleString (). substr (0,17)}
Alert (Getlocaltime (1293072805));
</script>

What if you want to get a format like this?
2010-10-20 10:00:00
Look at the code below.

The code is as follows Copy Code


<script>
function Getlocaltime (NS) {
return new Date (parseint (NS) * 1000). toLocaleString (). Replace (/year | month/g, "-"). Replace (/day/g, "");
}
Alert (Getlocaltime (1177824835));
</script>

It can be written like this.

The code is as follows Copy Code

function FormatDate (now) {
var year=now.getyear ();
var month=now.getmonth () +1;
var date=now.getdate ();
var hour=now.gethours ();
var minute=now.getminutes ();
var second=now.getseconds ();
return year+ "-" +month+ "-" +date+ "" +hour+ ":" +minute+ ":" +second; "
}

var d=new Date (1230999938);
Alert (FormatDate (d)); All right, problem solved.

It is to be noted that
Do not send a date (such as a character) in a string to be processed first, so it is convenient to handle
You can use the Replace method
As follows:

The code is as follows Copy Code

Replace ("/date", ""). Replace (")/", "");

Full instance

The code is as follows Copy Code
/**
* The same time stamp format function as PHP
* @param {string} format
* @param {int} timestamp the time that you want to format the current time
* @return {string} formatted 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 () {return 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 Date (jsdate.getfullyear () + "/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:function () {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 (); return (!) ( Y & 3) && (y% 1e2 | |!) ( Y% 4e2))? 1:0},
O Not supported yet
Y:function () {return jsdate.getfullyear ()},
Y:function () {return (Jsdate.getfullyear () + ""). 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 | | | 12},
G:function () {return jsdate.gethours ()},
H:function () {return pad (F.G (), 2)},
H:function () {return pad (jsdate.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
O:function () {
var 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 not supported yet
Z not supported 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:

The code is as follows Copy Code

Date (' y-m-d ', ' 1350052653 '); it's 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

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.