Commonly used date and time methods in js

1.Date()

Set the date to the current date. When an instance of a Date object is created without specifying a date, it returns a value that represents the current date and time, including year, month, day, hour, minute, second, and millisecond. You can then read or modify this date and time. The following example shows how to instantiate a date without any parameters and display it in the format YYYY-MM-DD hh:mm:ss. The format can be modified according to your needs.
Get the current year: var year = Date().getFullYear(); To

get the current month, you need to add 1: var month = Date().getMonth()+1;

Get the current date: var day = Date().getDate() ;

Get the current clock: var hour=Date().getHours();

Get the current minute: var minute=Date().getMinutes();

Get the current second: var second=Date().getSeconds();
Date() Common methods of:

var dt = new Date();
 var month = dt.getMonth()+1;
 var day = dt.getDate();
var year = dt.getFullYear();
var hour=dt.getHours();
 var minute=dt.getMinutes();
var second = dt.getSeconds ();
var time=year + '-' + month + '-' + day+' '+hour+':'+minute+':'+second; console.log(time); //2017-2-19 17:42:58

  2.moment()

Reference date handling library moment.js and its common formats.

The first format:

moment().format('YYYY MM DD, hh:mm:ss ');

//"2017 02 21, 01:52:42 "

 Second format:

moment().format('YYYY-MM-DD, hh:mm:ss ');


//"2017-02-21, 01:56:26 "

 The third format:

moment().format('YYYY MM month DD day, hh:mm:ss ');


//"February 21, 2017, 02:00:12 "

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326995796&siteId=291194637