Example of a JavaScript timestamp format

Source: Internet
Author: User

Scene:

Service-side output JSON:

{"Msglist": [{"MsgID": 1066, "Sendertype": "S", "SenderID": "The", "sender": "The", "Receivenum": 4, "Sendtime" : 1394422138000, "Msgflag": 1, "content": "{Title": "Cao", "Content": "Kao"}, "ContentType": 1},{"MsgID": 1067, " Sendertype ":" S "," SenderID ":" The "," Sender ":" 4 "," Receivenum ":" "," Sendtime ": 1395131639000," Msgflag ": 1," content ":" { "Title": "Cao", "Content": "Chinese Contents"}, "ContentType": 1}], "Noofpages": 1, "CurrentPage": 1}

The Sendtime value is a Java timestamp

JavaScript formats the timestamp, displaying the date as Yyyy-mm-dd h:m:s code as follows:

The code is as follows Copy Code


function Formattime (javatimestamp) {
var dt = new Date (javatimestamp);
var dt = new Date (unixtimestamp*1000);

var hours = pad (dt.gethours ());
var minutes = pad (dt.getminutes ());
var seconds = pad (Dt.getseconds ());

var years = Dt.getfullyear ();
var months = pad (Dt.getmonth () +1);
var days = pad (dt.getdate ());

Return years + "-" + Months + "-" + Days + "" +hours + ":" + minutes + ":" + seconds;
}

function Pad (value) {
Return (value.tostring (). length < 2)? ' 0 ' + value:value;
}

Call:

1394422138000 is a Java timestamp, Unix timestamp =java time stamp/1000

The code is as follows Copy Code

Formattime (1394422138000)

With a simple example

If you want to: 2010-10-20 10:00:00 in this format

  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>

Related Article

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.