Why write this article

Recently, when I was working on a project, I found that there were a lot of requirements to obtain the timestamp. Usually, when making a date selection, I needed to take the timestamp of the start time and the end time. Every time you have to Google it, you might as well do it yourself!

Gets the timestamp of the current moment

+ new Date()

Get today’s 0 and 24 time stamps

There is usually a need to see today’s data, so you need to get today’s 0 and 24 time stamps, which can be done with setHours

Zero:

SetHours (0, 0, 0, 0) let timestamp = new Date().sethours (0, 0, 0, 0Copy the code

24:

Let timestamp = new Date().sethours (24, 0, 0, 0)Copy the code

(PS: 24 o ‘clock on August 7th = 0 o ‘clock on August 8th hahaha) (PSS: what a boring PS)

Get a time stamp for the last seven days

In fact, the last seven days are from 0 o ‘clock six days ago to 24 o ‘clock today. For example, today is August 7, so to obtain the data of the last seven days, it should be from 00:00:00 on August 1 to 24:00 on August 7, and the time stamp of 24:00 on August 7 has been mentioned above, so I will not repeat it. What about 00:00:00 on August 1?

To get today's 0 o 'clock timestamp, subtract 6 days of milliseconds. We know that the number of milliseconds per day is 24 * 60 * 60 * 1000 so pass: Let sevenDaysBefore = new Date(). SetHours (0, 0, 0, 0) -6 * 24 * 60 * 60 * 1000Copy the code

(PS: Remember with me, nearly seven days, six days ago)

conclusion

Actually get the timestamp method still has a lot of, just write so much, in the next article I don’t know to write what, what want to see you can leave a message, I tried my best to understand write a write ha ha ha (while it’s still very vegetables, there are many things they don’t understand, but write articles like memory is very deep, ah, for her own ascension is big)

Then the timestamp conversion convenient website posted all: tool.chinaz.com/Tools/unixt…