About the form of the date obtained in JavaScript

Source: Internet
Author: User
Tags date1

1 getting the date format and its conversion
(1) var date = new Date ()

    Console.log (date); Wed 10:07:16 gmt+0800 (China Standard Time)
(2) var date = Date.now ()

    Console.log (date); 1493777310036
(3) var date = +new date (); IE9 the following is not supported

    Console.log (date); 1493777310036

In (1), if the result of date is Wed 10:07:16 gmt+0800 (China Standard Time),
Console.log (Date.tostring ()); Wed 10:10:56 gmt+0800 (China Standard Time)
Console.log (Date.todatestring ());// Wed May 03 2017
Console.log (Date.tolocaledatestring ()); 2017/5/3*/

2 Note:
var date1 = new Date (2017,04,01)//Represents May.

  var date1 = new Date (' 2017-5-1 ')//equals above
Console.log (Date1)
var date2 = new Date (1493777310036)
Console.log (date2)//wed may 10:08:30 gmt+0800 (China Standard Time)
var date3 = new Date ()
Console.log (date3.valueof ())//1493778043334

This millisecond number represents the number of milliseconds since 1970-01-01.
Returns the number of milliseconds corresponding to the time object, so you can use > < to determine the size of the two period

3 Gets the specified part of the date:
GetTime () returns the same number of milliseconds as the valueof () result
Getmilliseconds ()//MS
Getseconds () returns 0-59
Getminutes () returns 0-59
GetHours () returns 0-23
GetDay () returns day of the week 0 Sunday 6 weeks 6
GetDate () returns the day ordinal of the current month
GetMonth () returns the month, starting from 0
getFullYear () returns a 4-bit year, such as 2016

4 Package for 2017-05-01 12:12:12 format

function GetDateTime (DT) {

//Get all the elements we need first ~
             var year=dt.getfullyear ();
            var month=dt.getMonth () +1;
            var day=dt.getDate ();
            var h=dt.getHours ();
            var m=dt.getMinutes ();
            var s=dt.getSeconds ();

Here should think about, if is the number what to do? Then use ternary expressions to deal with
Month=month<10? " 0 "+month:month;
Day=day<10? " 0 "+day:day;
H=h<10? " 0 "+H:H;
M=m<10? " 0 "+M:M;
S=s<10? " 0 "+S:S;

The following can be returned
Return year+ "Year" +month+ "month" +day+ "Day" +h+ "when" +m+ "sub" +s+ "Seconds";
}

Put this method into our project's JS file, when I need to call directly on it.
GetDateTime (New Date ());

About the date that is obtained in JavaScript

Related Article

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.