📅  最后修改于: 2023-12-03 15:17:02.513000             🧑  作者: Mango
在 JavaScript 中,日期和时间以对象的形式表示。Date 可以表示日期和时间,其可以使用多种格式来创建和呈现。本文将介绍日期对象、日期的常用方法以及常见日期格式示例。
JavaScript 中的 Date 对象是用于处理日期和时间的对象。它可以通过“new Date()”来创建。
创建 Date 对象的方法有很多种,下面是一些常见的方法:
let curDate = new Date();
let curDate = new Date(1630065225000);
let curDate = new Date('2021-08-27');
let curDate = new Date('2021-08-27T12:30:00');
Date 对象拥有很多方法,可以获取和设置年份、月份、日期、时钟、分钟、秒钟等等。下面是一些常用方法:
let curDate = new Date();
let year = curDate.getFullYear();
let month = curDate.getMonth();
let date = curDate.getDate();
let hour = curDate.getHours();
let minute = curDate.getMinutes();
let second = curDate.getSeconds();
在 JavaScript 中,Date 对象可以用不同的方式呈现日期和时间。
下面是一些常用日期格式:
let curDate = new Date();
let formattedDate = `${curDate.getFullYear()}-${curDate.getMonth() + 1}-${curDate.getDate()}`;
let curDate = new Date();
let formattedDate = `${curDate.getMonth() + 1}/${curDate.getDate()}/${curDate.getFullYear()}`;
let curDate = new Date();
let formattedDate = `${curDate.getDate()}/${curDate.getMonth() + 1}/${curDate.getFullYear()}`;
JavaScript 中的 Date 对象可以用多种方式表示日期和时间。本文介绍了 Date 对象的基本用法和常见的日期格式示例。大家可以根据自己的需求选择对应的日期格式来处理日期和时间。