Home  >  Article  >  Web Front-end  >  Summary of time processing in JavaScript_javascript skills

Summary of time processing in JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:13:391333browse

No more nonsense, I mainly summarize the knowledge related to time processing through the following seven aspects.

1. Get the current time

function getNowTime() {
return new Date();
}

2. Add time and 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 day of the week

var WEEKDAYS = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"]; //星期
function getFormatWeek(time) {
return WEEKDAYS[time.getDay()];
}

6. Time comparison

function compareTime(time1, time2) {
return time1.getTime() - time2.getTime();
}

7. Calculate the number of days between two dates

function getDays(time1, tiem2){
var day = 24*60*60*1000;
return (time1.getTime() - time2.getTime())/day;
}

The editor has summarized seven aspects of knowledge about time processing in js. I hope it will be helpful to you!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn