Time Processing Summary In JavaScript and javascript processing summary
I will not talk much about it. I will summarize the time processing knowledge in 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 week
Var WEEKDAYS = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; // week 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;}
I have summarized seven aspects of js time processing knowledge and hope to help you!
Articles you may be interested in:
- Function Code for processing the current interval in JS
- Js uses the Date object to process time Implementation ideas and code
- How to process timestamp in javascript in Date Format
- How to deal with php output timestamp mismatch in js
- Example of processing the serialization time of JavaScript Serializer