Home > Web Front-end > JS Tutorial > body text

Summary of time processing in JavaScript_javascript skills

WBOY
Release: 2016-05-16 15:13:39
Original
1329 people have browsed it

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();
}
Copy after login

2. Add time and days

function getTimeAddDays(time, days) {
return new Date(time.getTime() + days * 24 * 60 * 60 * 1000);
}
Copy after login

3. Get and format the date: year-month-day

function getFormatDate(time) {
return time.getFullYear() + "-" + (time.getMonth() + 1) + "-" + time.getDate();
}
Copy after login

4. Convert string to date, string format: 2011-11-20

function convertToDate(strings) {
return new Date(Date.parse(strings.replace("-", "/")));
}
Copy after login

5. Get and format the day of the week

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

6. Time comparison

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

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;
}
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!