Time Processing in JavaScript _ javascript skills

Source: Internet
Author: User
This article mainly introduces the related information about the time processing summary in JavaScript. If you need more information, I will summarize the time processing knowledge 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 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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.