Convert the timestamp (in Milliseconds) from the background to the desired date format with JS

Source: Internet
Author: User

Get the data from the database in the background we want the format to Be:

October 25, 2016 17:37 30 seconds or 2016/10/25 17:37:30

But what we get at the front desk is a number (timestamp, number of milliseconds):

1477386005

Timestamp conversion, The core method :

1477386005 is to get a timestamp from the background  (note: Sometimes the timestamp is multiplied by 1000)
var unixtimestamp = new Date (1477386005*1000); commontime = unixtimestamp.tolocalestring ();
Alert (commontime);

The result is:

    

Rewrite the toLocaleString () method to change to any format:

Date.prototype.toLocaleString = function () {          return this.getfullyear () + "year" + (this.getmonth () + 1) + "month" + this.getd Ate () + "day" + this.gethours () + "point" + this.getminutes () + "min" + this.getseconds () + "sec";    };

The result is:

    

Other formats:

Date.prototype.toLocaleString = function () {          return this.getfullyear () + "/" + (this.getmonth () + 1) + "/" + this.getd Ate () + "/" + this.gethours () + ":" + this.getminutes () + ":" + this.getseconds ();    };

The result is:

article reference: lemon Whirlwind Leg

links: time stamp conversion website W3school

Convert the timestamp (in Milliseconds) from the background to the desired date format with JS

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.