JS Timestamp in Get mode

Source: Internet
Author: User
Tags dateformat

The meaning of a time stamp

Time stamp refers to the total number of seconds GMT January 01, 1970 00:00 00 seconds (Beijing time January 01, 1970 08:00 00 seconds) up to now.

Two, 1. JS Gets the current timestamp method:

var timestamp = date.parse (new Date ()),//deprecated, because the millisecond level value is converted to 000, inaccurate!

var timestamp = (new Date ()). ValueOf ();//Gets the timestamp of the current millisecond, exactly!

var timestamp = new Date (). Get time ();//return value unit is milliseconds;

How to get the specified timestamp in 2.js

var timestamp = (New Date ("2018/06/22 08:00:20")). GetTime ()/1000;

Iii. time conversion into timestamps

var timestamp= new Date (1472025255555)//directly with the new date (timestamp to convert);

Note that the timestamp must be int type, otherwise the conversion fails. parseint () a bit better.

Iv. Date of format

The development of the long use to date, but you new date () or obtained can not be directly used to display to the user, so generally we will write a method of formatting the date

As follows:

Formatted date
function DateFormat (thisdate, fmt) {var o = {"m+": Thisdate.getmonth () + 1, "d+": thisdate.getdate (), "H +": thisdate.gethours (), "m+": Thisdate.getminutes (), "s+": Thisdate.getseconds (), "q+": Math    . Floor ((Thisdate.getmonth () + 3)/3), "S": Thisdate.getmilliseconds ()};    if (/(y+)/.test (FMT)) FMT = Fmt.replace (regexp.$1, (thisdate.getfullyear () + ""). substr (4-regexp.$1.length)); For (var k in O) if (New RegExp ("(" + K + ")"). Test (FMT)) FMT = Fmt.replace (regexp.$1, (regexp.$1.lengt h = = 1)?    (O[k]): (("XX" + o[k]). substr (("" + O[k]).)); return FMT;}
var date=new Date ();
Console.log (DateFormat (Date, "Yyyy-mm-dd hh:mm:ss")//Call

Five, according to the date to calculate the week


1. Calculate the current date for Monday and Sunday

var date=new Date ();
var selecttime = Date.gettime ();//Get timestamp
var selectday = Date.getday ();//Get week var onedaylong = 24 * 3600 * 1000;//defines how many milliseconds a day is var mondaytime = selecttime-(SE LECTDAY-1) * Onedaylong;
var Monday = new Date (mondaytime)//timestamp back to TIME
var sundaytime = selecttime-(selectDay-7) * Onedaylong;
var sundaytime = new Date (sundaytime)

Console.log (Monday)
Console.log (Sundaytime)

2. Calculate the day of the week

When we know a certain ' yyyy-mm-dd ' date we can calculate the current day of the week, the formula is as follows:

Kimlarsson Calculation formula
W= (d+2*m+3* (m+1)/5+y+y/4-y/100+y/400) MoD 7
In the formula, D represents the number of days in a date, m represents the number of months, and Y represents the number of years. (which mod means to take the remainder calculation, JS with%)

     

JS Timestamp in Get mode

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.