The use of Date objects in JS

Source: Internet
Author: User
Tags date1

A: What is the Date object?

The Date object is used to process dates and times.

Two: Date creation syntax

1. Direct access to the current time is standard Time

var date = new Date ()

Console.log (date)//tue Dec 23:09:42 gmt+0800 (China Standard Time)

Note: The Date object automatically saves the current date and time as its initial value.

2. Specify time to change to standard time

var date1 = new Date ("January 12,2006 22:19:35")

Console.log (date1)//thu Jan 2006 22:19:35 gmt+0800 (China Standard Time)

Several of the following formats are available:

New Date ("Month dd,yyyy hh:mm:ss");
New Date ("Month dd,yyyy");
New Date (YYYY,MTH,DD,HH,MM,SS);
New Date (YYYY,MTH,DD);
New Date (MS);

Three: Common Object methods

Date.getfullyear ()//2017 returns the year as a four-digit number from a Date object

Date.getmonth ()//11 returns the month from the Date object (0 ~ 11)

Date.getday ()//2 returns one day of the week from a Date object (0 ~ 6)

Date.getdate ()//12 returns one day in one months from a Date object (1 ~ 31)

Date.gethours ()//23 returns the hour of the date object (0 ~ 23)

Date.getminutes ()//30 returns the minute of the Date object (0 ~ 59)

Date.getseconds ()//54 returns the number of seconds of the date object (0 ~ 59)

Date.getmilliseconds () returns the milliseconds of the Date object (0 ~ 999)

Date.gettime () returns the number of milliseconds since January 1, 1970

Four: each case conversion

1. var time = "2017-12-12" converted to millisecond timestamp

New Date (Time.replace (/-/g, "/")). GetTime ()//1513008000000

2. var time = 1513008000000 converted to "2017-12-12"

Note: Time cannot be a string if number

var date = new Date (time)

var year1 = Date2.getfullyear ()
var month = Date2.getmonth ()
var today = Date2.getdate ()
var Time1 = year1 + "-" + month + "-" + Today

The use of Date objects in JS

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.