JS converts time to timestamp

Source: Internet
Author: User

Transfer from Http://zhidao.baidu.com/link?url=jwmRLUKIC92fNeS1l8PuZltmZIN--LJFtKd9G6SYEjFfCu_pFGyXsh54txzv22E0gdWvoHrFX39t5-U_ntf43K

Specific time stamp How to define I also do not know, but Baidu Encyclopedia has such a sentence: "Timestamp is the total number of seconds from January 1, 1970 (00:00:00 GMT) to the current time".

According to this definition, there is a similar function in the programming language, GetTime (), but this function returns the total number of milliseconds from January 1, 1970 to the current time , not the total number of seconds.

In JS, it is not difficult to convert a character into a date type:
var str = ' 2013-08-30 '; Date string

str = str.replace (/-/g, '/'); Will-replace with/, because the following constructor only supports/separates date strings

var date = new Date (str); Constructs a date-type data with a value of the passed-in string

Above, the new date (str) constructs a date, and the parameter STR must provide at least three parts of the month and day, that is, a string that is shaped like "2013/03/08", and cannot be "2013/03", otherwise it will get a nan. The time to construct this is: 2013/03/08 00:00:00. You can also pass in hours, minutes, and seconds, but not just the hours, such as "2013/03/08 17", and the parameters will also get a Nan. The parameter can be "2013/03/08 17:20" or "2013/03/08 17:20:05", so that you can get the correct time, where if the number of seconds is not given, the default is 0.

At this point the date data is obtained, if you want to get the so-called timestamp, you can:
var time = Date.gettime ();
This gets a numeric value that represents the number of milliseconds from January 1, 1970 0:0 0 seconds to the moment of date, and if you divide this number by 1000, you get the number of seconds, and you continue to divide by 60, get the minute, divide by 60 to get the hour, and so on.

Tip, with this gettime () function, you can get the number of milliseconds for two dates, then convert to seconds, minutes, hours, or even days, comparing the difference of two dates.

JS converts time to timestamp

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.