JavaScript Date 日期与时间,时区、时间戳;时间戳转日期格式+日期格式转时间戳,介绍

文章目录


前言

什么是时间戳?时间戳的作用是什么?

介绍:一个能表示一份数据在某个特定时间之前已经存在的、 完整的、 可验证的数据,通常是一个字符序列,唯一地标识某一刻的时间。一般来说时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。js 时间戳是毫秒长度是13位   秒的时间戳是10位(说白了就是表示某一刻的时间)

作用:在互联网公司都会在项目种使用时间戳,时间戳主要用于清理缓存,大多数用于版本更新;同时在开发中使用时间戳进行时间段筛选,时间点判断等


一、格林威治时间(UTC)概念

UTC 协调世界时即格林威治平太阳时间,是指格林威治所在地的标准时间,也是表示地球自转速率的一种形式,UTC基于国际原子时间。

在UTC的标准上结合各个时区的时间差,即可算出当地时间(东N区便+N小时,西N区便-N小时)。

二、日期和时间(关键字)

三、相关获取方法

let timeDate = new Date('2022/09/20 10:15:20');
console.log(timeDate.getFullYear()); //2022 年
console.log(timeDate.getMonth()); //9 月 需要+1
console.log(timeDate.getDate()); //20 日
console.log(timeDate.getHours()); //10 时
console.log(timeDate.getMinutes()); //15 分
console.log(timeDate.getSeconds()); //20 秒
console.log(timeDate.getMilliseconds()); //0 毫秒

1.getFullYear()    获取年份

    从 Date 对象以四位数字返回年份。

  • 语法
    Date.getFullYear()
    • return 返回年份
  • 示例
    //需求:获取年份
    let timeDate = new Date('2022/09/20 10:15:20');
    console.log(timeDate.getFullYear()); //2022
    
     

2.getMonth()    获取月份

 从 Date 对象返回月份 (0 ~ 11);其中,0 为一月

  • 语法
    Date.getMonth()
    • return 返回几月
  • 示例
    //需求:获取月份
    let timeDate = new Date('2022/09/20 10:15:20');
    console.log(timeDate.getMonth()); //8
    

3.getDate() 获取几号

 从 Date 对象返回一个月中的某一天 (1 ~ 31)

  • 语法
    Date.getDate()
    • return 返回几号
  • 示例
    //需求:获取几号
    let timeDate = new Date('2022/09/20 10:15:20');
    console.log(timeDate.getDate()); //20

4.getHours() 获取小时

返回 Date 对象的小时 (0 ~ 23)。

  • 语法
    Date.getHours()
    • return 返回小时
  • 示例
    //需求:获取小时
    let timeDate = new Date('2022/09/20 10:15:20');
    console.log(timeDate.getHours()); //10

5.getMinutes() 获取分钟

 返回 Date 对象的分钟 (0 ~ 59)。

  • 语法
    Date.getMinutes()
    • return 返回分钟
  • 示例
    //需求:获取分钟
    let timeDate = new Date('2022/09/20 10:15:20');
    console.log(timeDate.getMinutes()); //15
    

6.getSeconds() 获取秒数

 返回 Date 对象的秒数 (0 ~ 59)。

  • 语法
    Date.getSeconds()
    • return 返回秒
  • 示例
    //需求:获取秒
    let timeDate = new Date('2022/09/20 10:15:20');
    console.log(timeDate.getSeconds()); //20
    

7.getMilliseconds() 获取毫秒

 返回 Date 对象的毫秒(0 ~ 999)。

  • 语法
    Date.getMilliseconds()
    • return 返回毫秒
  • 示例
    //需求:获取毫秒
    let timeSecond = new Date(1626486293732);
    console.log(timeSecond.getMilliseconds()); //732
    

8.getTime() 时间戳

返回 1970 年 1 月 1 日至今的毫秒数,及时间戳。

  • 语法
    Date.getTime()
    • return 返回时间戳。
  • 示例
    //需求:获取时间戳
    let times = new Date('2021/07/17 09:44:53');
    console.log(times.getTime()); //1626486293000
    

9.getDay() 获取周几

从 Date 对象返回一周中的某一天 (0 ~ 6)。
其中,0 为周日。

  • 语法
    Date.getDay ()
    • return 返回周几。
  • 示例
    //需求:获取周几
    let timeDate = new Date('2021/07/17 09:44:53');
    console.log(timeDate.getDay()); //6
    

10.parse ``

返回1970年1月1日午夜到指定日期(字符串)的毫秒数。

四、相关设置方法

1. setFullYear ()设置年份
        设置 Date 对象中的年份(四位数字)。

2. setMonth() 设置月份
        设置 Date 对象中月份 (0 ~ 11)。

3. setDate() 设置日期
        设置 Date 对象中月的某一天 (1 ~ 31)。

4. setHours() 设置小时
        设置 Date 对象中的小时 (0 ~ 23)。

5. setMinutes() 设置分钟
        设置 Date 对象中的分钟 (0 ~ 59)。

6. setSeconds() 设置秒数
        设置 Date 对象中的秒钟 (0 ~ 59)。

7. setMilliseconds() 设置毫秒
        设置 Date 对象中的毫秒 (0 ~ 999)。

8. setTime() 设置时间戳
        以时间戳来设置 Date 对象。

五、UTC获取

let timeDate = new Date('2022/09/20 10:44:35');
console.log(timeDate.getUTCFullYear());     //2022 年
console.log(timeDate.getUTCMonth());        //9 月 需要+1
console.log(timeDate.getUTCDate());         //20 日
console.log(timeDate.getUTCHours());        //2 时 北京为8时区,所以10-8=2
console.log(timeDate.getUTCMinutes());      //44 分
console.log(timeDate.getUTCSeconds());      //35 秒
console.log(timeDate.getUTCMilliseconds();  //0 毫秒

1. getUTCFullYear() 获取年份
根据世界时从 Date 对象返回四位数的年份。

2. getUTCMonth() 获取月份
根据世界时从 Date 对象返回月份 (0 ~ 11)。

3. getUTCDate ()获取日期
根据世界时从 Date 对象返回月中的一天 (1 ~ 31)。

4. getUTCHours ()获取小时
根据世界时返回 Date 对象的小时 (0 ~ 23)。

5. getUTCMinutes ()获取分钟
根据世界时返回 Date 对象的分钟 (0 ~ 59)。

6. getUTCSeconds() 获取秒数
根据世界时返回 Date 对象的秒钟 (0 ~ 59)。

7. getUTCMilliseconds() 获取毫秒
根据世界时返回 Date 对象的毫秒(0 ~ 999)。

8. getTimezoneOffset() 时区差值
返回本地时间与格林威治标准时间 (GMT) 的分钟差。

六、UTC设置      

1. setUTCFullYear() 
根据世界时设置 Date 对象中的年份(四位数字)。

2. setUTCMonth ()月
根据世界时设置 Date 对象中的月份 (0 ~ 11)。

3. setUTCDate()
根据世界时设置 Date 对象中月份的一天 (1 ~ 31)。

4. setUTCHours ()
根据世界时设置 Date 对象中的小时 (0 ~ 23)。

5. setUTCMinutes ()
根据世界时设置 Date 对象中的分钟 (0 ~ 59)。

6. setUTCSeconds()
根据世界时设置 Date 对象中的秒钟 (0 ~ 59)。

7. setUTCMilliseconds() 毫秒

根据世界时设置 Date 对象中的毫秒 (0 ~ 999)。

七、相关转换      

1. toString

把 Date 对象转换为字符串。

let timeData = new Date('2021/07/17 09:44:53');
console.log(timeData.toString()); //Sat Jul 17 2021 09:44:53 GMT+0800 (中国标准时间)

2. toTimeString

把 Date 对象的时间部分转换为字符串。

let timeDate = new Date('2021/07/17 09:44:53');
console.log(timeDate .toTimeString()); //09:44:53 GMT+0800 (中国标准时间)

3. toDateString

把 Date 对象的日期部分转换为字符串。

let timeDate  = new Date('2021/07/17 09:44:53');
console.log(timeDate .toDateString()); //Sat Jul 17 2021

4. toUTCString

根据世界时,把 Date 对象转换为字符串。

let timeDate  = new Date('2022/10/20 10:55:15');
console.log(timeDate .toUTCString()); //Sat, 20 oct 2022 10:55:15 GMT

5. toLocaleString

根据本地时间格式,把 Date 对象转换为字符串。

let timeDate  = new Date('2022/10/20 10:55:15');
console.log(timeDate .toLocaleString()); //2022/10/20 上午10:55:15
//这个方法还是比较实用,可以很容易的读懂时间

6. toLocaleTimeString

根据本地时间格式,把 Date 对象的时间部分转换为字符串。

let timeDate  = new Date('2022/10/20 10:55:15');
console.log(timeDate .toLocaleTimeString()); //上午10:55:15

7. toLocaleDateString

根据本地时间格式,把 Date 对象的日期部分转换为字符串。

let timeDate  = new Date('2022/10/20 09:44:53');
console.log(timeDate .toLocaleDateString()); //2022/10/20

七、方法封装+实例

        1.JavaScript 获取当前时间戳:

                第一种方法:

var timestamp = Date.parse(new Date());
结果:1280977330000

                第二种方法:

var timestamp = (new Date()).valueOf();
结果:1280977330748

                第三种方法:

var timestamp=new Date().getTime();
结果:1280977330748

第一种:获取的时间戳是把毫秒改成000显示,第二种和第三种是获取了当前毫秒的时间戳。

JavaScript中valueOf函数

        JavaScript中valueOf函数方法是返回指定对象的原始值。使用方法:

object.valueOf( )

自定义方法

/**
 * 日期格式转时间戳
 * date: 传入的日期格式
 */
function timeNumber(date) {
  let year, month, day, hour, minute, second;

  year = date.getFullYear();
  month = date.getMonth();
  day = date.getDate();

  hour = date.getHours();
  minute = date.getMinutes();
  second = date.getSeconds();

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString();
  return n[1] ? n : '0' + n;
}

/**
 * 时间戳转化为年,月,日,时,分, 秒
 * number: 传入时间戳
 * format: 返回格式,支持自定义,但参数必须与formateArr里保持一致
 */

function numberTime(number, format) {
  let formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  let returnArr;

  let date = new Date(number * 1000);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));

  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));

  for (let i in returnArr) {
    fromat = format.replace(fromateArr[i],returnArr[i]);
  }
  return format;
}


module.exports = {
  timeNumber: timeNumber,
  numberTime: numberTime
}


七、注意事项

  • 获取月份时,实际月数因为数值+1。
  • 获取周几时,0表示的周日。
  • iOS上时间不能为24:00:00,应该为23:59:59,否则为报错;但是部分浏览器支持该写法。
  • iOS上字符串转日期时,尽至此格式yyyy/mm/dd
//兼容iOS
let datestr = '2021-07-17 09:44:53';
datestr = datestr.replace(/-/g, '/');

总结

常用时间戳:

一小时的时间戳
3600000
一天的时间戳
86400000
一个月的时间戳
2592000000
一年的时间戳
31104000000

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值