Soft front-end technology classroom: JavaScript Date

JavaScript date output

By default, JavaScript will use the browser's time zone and date is displayed as full-text string:

Tue Apr 02 2019 09:01:19 GMT + 0800 (China Standard Time)

Create a Date object

Date object by the new  () constructor to create a Date.

There are four ways to create a new target date:

  • new Date()
  • new Date(year, month, day, hours, minutes, seconds, milliseconds)
  • new Date(milliseconds)
  • new Date(date string)

new Date()

new Date () Creates a new date object with the current date and time:

Examples

var d = new Date();

 

Date objects are static. Computer time is ticking, but the date is not the object.

new Date(year, month, ...)

new Date (year, month, ... )  to create a new date object with the specified date and time.

7 Digital specify the year, month, day, hour, minute, seconds and milliseconds (in this order):

Examples

var d = new Date(2018, 11, 24, 10, 33, 30, 0);

Notes : JavaScript is calculated from January 0-11.

January is 0. December 11.

6 digit year, month, day, hour, minute, second:

Examples

var d = new Date(2018, 11, 24, 10, 33, 30);

 

5 digit year, month, day, hour and minute:

Examples

var d = new Date(2018, 11, 24, 10, 33);

 

4 digit year, month, day and hour:

Examples

var d = new Date(2018, 11, 24, 10);

 

3 numbers specify the year, month and day:

Examples

var d = new Date(2018, 11, 24);

 

2 number specifies the month and year:

Examples

var d = new Date(2018, 11);

 

You can not omit the month. If only one parameter, it is considered milliseconds.

var d = new Date(2018);

 

Date objects are static. Computer time is ticking, but the date is not the object.

new Date(year, month, ...)

new Date (year, month, ... )  to create a new date object with the specified date and time.

7 Digital specify the year, month, day, hour, minute, seconds and milliseconds (in this order):

Examples

var d = new Date(2018, 11, 24, 10, 33, 30, 0);

 

new Date(dateString)

new Date (dateString) to create a new date object from a date string:

Examples

var d = new Date("October 13, 2014 11:13:00");

JavaScript 将日期存储为毫秒

JavaScript 将日期存储为自 1970 年 1 月 1 日 00:00:00 UTC(协调世界时)以来的毫秒数。

零时间是 1970 年 1 月 1 日 00:00:00 UTC。

现在的时间是:1970 年 1 月 1 日之后的 1554166879383 毫秒。

new Date(milliseconds)

new Date(milliseconds) 创建一个零时加毫秒的新日期对象:

实例

var d = new Date(0);

1970年 1 月 1 日加上100 000 000 000毫秒,大约是 1973 年 3 月 3 日:

实例

var d = new Date(100000000000);

1970 年 1 月 1 日减去 100 000 000 000 毫秒大约是 1966 年 10 月 31 日:

实例

var d = new Date(-100000000000);

日期方法

创建 Date 对象时,可以使用许多方法对其进行操作。

日期方法允许您使用本地时间或 UTC(通用或 GMT)时间来获取和设置日期对象的年、月、日、小时、分钟、秒和毫秒。

显示日期

JavaScript(默认情况下)将以全文本字符串格式输出日期:

Wed Mar 25 2015 08:00:00 GMT+0800 (中国标准时间)

 

在 HTML 中显示日期对象时,会使用 toString() 方法自动转换为字符串。

实例

d = new Date();
document.getElementById("demo").innerHTML = d;

等同于:

d = new Date();
document.getElementById("demo").innerHTML = d.toString();

toUTCString() 方法将日期转换为 UTC 字符串(一种日期显示标准)。

实例

var d = new Date();
document.getElementById("demo").innerHTML = d.toUTCString();

toDateString() 方法将日期转换为更易读的格式:

实例

var d = new Date();
document.getElementById("demo").innerHTML = d.toDateString();

文章来源:www.sysoft.net.cn,加v:15844800162深度交流

Guess you like

Origin www.cnblogs.com/sysoft/p/11702767.html