Time is converted to timestamp

Gets the timestamp of the current time

var now_timestamp = Date.parse(new Date());
Copy the code

Time stamps for each unit of time

The official definition of a timestamp is a time representation of the total number of seconds from 00 hours 00 minutes 00 seconds GMT, January 01, 1970 to the present. I use JS to get the timestamp, and the printed output retains the last 000 milliseconds. To facilitate some calculations, I use milliseconds to store the value of each unit of time stamp.

var minute_timestamp = 60 * 1000; 			/ / 1 minutes
var day_timestamp = 24 * 60 * 60 * 1000; 		/ / 1 day
var week_timestamp = day_timestamp * 7; 		/ / 7 days
Copy the code

A time turn timestamp is available

var get_timestamp = (new Date("2020/12/24 11:59:59")).getTime()
Copy the code

GetTime () returns a value in milliseconds;

The timestamp is converted to time

Convert the timestamp time of the current time;

//now_timestamp is an integer, otherwise parseInt is required
var time = new Date(now_timestamp);
Copy the code

Conversion of year, month and day (format: XXXX, month and day)

Several apis, getFullYear(), getMonth(), getDate();

/ / year
var year = time.getFullYear().toString();
// month, less than 10 is preceded by a 0 by default
var month = (time.getMonth() + 1).toString();
if (month.length < 2) {month = "0"+ month; }// day, less than 10 is preceded by a 0 by default
var day = time.getDate().toString();
if (day.length < 2) {day = "0"+ day; }Copy the code

The day of the week switch

Get the data from getDay(), Sunday-Saturday (0-6);

var weekdays = ["Sunday"."Monday"."Tuesday"."Wednesday"."Thursday"."Friday"."Saturday",];
var weekday = time.getDay().toString();
Copy the code