JS里的与日期和时间有关的对象
Date本身是一个函数, 我们可以通过它创建一个日期时间对象通过该对象来操作与日期或者时间有关的方法
基本方法
获取当前时间
new Date()
Date.now()
获取年份
new Date().getFullYear()
获取月份
取值范围0-11
new Date().getMonth()
获取周
周日-周六 0-6
new Date().getDay()
获取日
new Date().getDate()
获取小时
new Date().getHours()
获取分钟
new Date().getMinutes()
获取秒
new Date().getSeconds()
注:new Date()的 方法中,可以添加时间戳来获取
new Date().getFullYear()
例:new Date(898870000000).getFullYear() //1998
基础例子:
var dateObj = new Date();
"当前时间:" + dateObj.getHours()+ ":" + dateObj.getMinutes() + ":" + dateObj.getSeconds();
var futureDate = new Date(2021,2,20,18,0,0);
console.log(futureDate);
时间格式化转换
dateObj.toLocaleString([locales, [options]])
下面展示的是自己常用的,有些没添加方法,等以后用到了再加
locales 语系
用于指定格式化对象时使用的语言环境
zh
en
常用这两个,除非你们业务扩展很牛,那我就没办法了
const date = new Date();
new Date().toLocaleString('zh')
//2024/5/15 16:52:26
console.log(date.toLocaleString(`en-US`));
// 11/10/2019, 4:32:44 PM
console.log(date.toLocaleString(`hi-IN`));
// 10/11/2019, 4:32:44 pm
console.log(date.toLocaleString(`fr-CH`));
// 10.11.2019 à 16:32:44
const options = {
weekday: 'long',
era: 'long'
}
console.log(date.toLocaleString(`en-US`, options));
// Sunday Anno Domini
console.log(date.toLocaleString(`hi-IN`, options));
// ईसवी सन रविवार
console.log(date.toLocaleString(`fr-CH`, options));
// après Jésus-Christ dimanche
options 设置
注:下面皆以‘zh’为操作,谁让我爱国呢
hour12
该属性用来控制返回的时间是12小时制还是24小时制,它的值是布尔类型(true或false)。默认是true,即返回的时间是12小时制。
var date = new Date();
console.log(date.toLocaleString('zh', {hour12: true}));
//2024/5/15 下午4:29:45
console.log(date.toLocaleString('zh', {hour12: false}));
//2024/5/15 16:29:45
formatMatcher
格式匹配器
- short
var date = new Date();
console.log(date.toLocaleString('zh', { timeZoneName: 'short' }));
// 2024/5/15 GMT+8 16:30:17
- long
console.log(date.toLocaleString('zh', { timeZoneName: 'long' }));
// 2024/5/15 中国标准时间 16:30:45
era
纪元,太大了,我实在活不到
- long
- short
- narrow
var date = new Date();
console.log(date.toLocaleString('zh', { era: 'long' }));
// 公元 2024-05-15 16:26:47
year
-
numeric
-
2-digit 简单的说就是是否可以用两个数字表示
var date = new Date();
console.log(date.toLocaleString('zh', {year: 'numeric'}));
// 2024年
console.log(date.toLocaleString('zh', {year: '2-digit'}));
// 24年
month
-
numeric
-
2-digit 简单的说就是是否可以用两个数字表示
-
long
-
short
-
narrow
new Date().toLocaleString('zh', { month: 'numeric'})
//5月
new Date().toLocaleString('zh', { month: '2-digit'})
//05月
new Date().toLocaleString('zh', { month: 'long'})
// 五月
new Date().toLocaleString('zh', { month: 'short'})
// 5月
new Date().toLocaleString('zh', { month: 'narrow'})
// 5
weekday
- long
- short
- narrow
new Date().toLocaleString('zh', { weekday: 'long'})
// 星期三
new Date().toLocaleString('zh', { weekday: 'short'})
// 周三
new Date().toLocaleString('zh', { weekday: 'narrow'})
// 三
day
-
numeric
-
2-digit 简单的说就是是否可以用两个数字表示
new Date().toLocaleString('zh', { day: 'numeric'})
// 5日
new Date().toLocaleString('zh', { day: '2-digit'})
// 05日
hour
-
numeric
-
2-digit 简单的说就是是否可以用两个数字表示
new Date().toLocaleString('zh', { hour: '2-digit'})
//16时
minute
奇怪的是我这两个类型没区别,我很疑惑
-
numeric
-
2-digit 简单的说就是是否可以用两个数字表示
new Date().toLocaleString('zh', { minute: 'numeric'})
// 46
new Date().toLocaleString('zh', { minute: '2-digit'})
// 46
second
奇怪的是我这两个类型没区别,我很疑惑
-
numeric
-
2-digit 简单的说就是是否可以用两个数字表示
new Date().toLocaleString('zh', { second: 'numeric'})
//24
new Date().toLocaleString('zh', { second: '2-digit'})
//24
timeZoneName
区时名
- long
- short
new Date().toLocaleString('zh', { timeZoneName: 'short'})
//2024/5/15 GMT+8 16:58:02
new Date().toLocaleString('zh', { timeZoneName: 'long'})
//2024/5/15 中国标准时间 16:59:08
localeMatcher
指定要使用的区域设置匹配算法。
hourCycle
综合写法
zh
new Date().toLocaleString('zh', {
year: 'numeric',
month: 'numeric',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric'
})
//2024/5/15 17:08:27
new Date().toLocaleString('zh', {
year: '2-digit',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
})
//24/05/15 17:09:49