Date对象是JavaScript语言中内置的数据类型。日期对象使用新的Date()创建,如下所示。
创建Date对象后,可以使用多种方法对其进行操作。大多数方法仅允许您使用本地时间或UTC(通用或GMT)时间来获取和设置对象的年,月,日,时,分,秒和毫秒字段。
setUTCDate()date对象的功能接受一个表示日期(月中的某天)的整数,并根据通用时间用它替换当前日期的值。
它的语法如下
dateObj.setUTCDate(19);
<html>
<head>
<title>JavaScript Example</title>
</head>
<body>
<script type="text/javascript">
var dateObj = new Date('September 26, 89 12:4:25:96');
document.write("Current date: "+dateObj.toUTCString());
document.write("<br>");
dateObj.setUTCDate(30);
document.write("Modified date: "+dateObj.toUTCString());
</script>
</body>
</html>输出结果
Current date: Tue, 26 Sep 1989 06:34:25 GMT Modified date: Sat, 30 Sep 1989 06:34:25 GMT
尽管在创建日期对象时未提及月份的日期,但仍可以使用setUTCDate()函数进行设置。
<html>
<head>
<title>JavaScript Example</title>
</head>
<body>
<script type="text/javascript">
var dateObj = new Date('September, 1989 00:4:00');
dateObj.setUTCDate(2);
document.write("<br>");
document.write(dateObj.getUTCDate());
</script>
</body>
</html>输出结果
2
同样,尽管在创建日期对象时未将任何值传递给构造函数,但仍可以使用此函数设置UTC日期,并且蛾和年值与当前日期相同。
<html>
<head>
<title>JavaScript Example</title>
</head>
<body>
<script type="text/javascript">
var dateObj = new Date();
dateObj.setUTCDate(2);
document.write("<br>");
document.write("UTC month: "+dateObj.getUTCMonth());
document.write("<br>");
document.write("UTC date: "+dateObj.getUTCDate());
document.write("<br>");
document.write("UTC fullyear: "+dateObj.getUTCFullYear());
</script>
</body>
</html>输出结果
UTC month: 9 UTC date: 2 UTC full year: 2018