Summary of time processing in JavaScript
- 2021-01-11 01:51:08
- OfStack
Nonsense is not much said, mainly through the following 7 aspects to summarize the time to deal with relevant knowledge.
1. Get the current time
function getNowTime() {
return new Date();
}
2. Add time to days
function getTimeAddDays(time, days) {
return new Date(time.getTime() + days * 24 * 60 * 60 * 1000);
}
3. Get and format the date: Year-Month-Day
function getFormatDate(time) {
return time.getFullYear() + "-" + (time.getMonth() + 1) + "-" + time.getDate();
}
4. Convert string to date, string format: 2011-11-20
function convertToDate(strings) {
return new Date(Date.parse(strings.replace("-", "/")));
}
5. Get and format the week
var WEEKDAYS = [" Sunday ", " weeks 1", " weeks 2", " weeks 3", " weeks 4", " weeks 5", " weeks 6"]; // week
function getFormatWeek(time) {
return WEEKDAYS[time.getDay()];
}
6. Time comparison
function compareTime(time1, time2) {
return time1.getTime() - time2.getTime();
}
7. Count the number of days between two dates
function getDays(time1, tiem2){
var day = 24*60*60*1000;
return (time1.getTime() - time2.getTime())/day;
}
This site gives you a summary of 7 aspects of js time processing knowledge, I hope to help you!