1 获取时间戳

1.获取系统当前时间的时间戳(单位:毫秒)

方法1:
var timestamp1= +new Date();
方法2:
var timestamp2 = new Date().valueOf();
方法3:
var timestamp3 = new Date().getTime();

2.获取特定时间的时间戳
var timestamp = new Date(‘2021-08-22 20:32:08’).getTime();

2 将时间戳转为特定格式的日期

如:YYYY-MM-DD HH:mm:ss

rule result
YYYY:MM:DD 2021年08月22日
YY年MM月DD日 H时m分s秒 21年08月22日 20时45分17秒或 21年08月22日 20时46分3秒
YYYY-MM-DD HH:mm:ss 2021-08-22 20:46:03

即:
YYYY 2021,YY 21
MM 08 或12
M 8或12
其他与MM和M规则相同
实现代码:

var now = +new Date();
console.log(now); //得到当前时间戳console.log('无规则-----' + moment(now));
console.log('YY年MM月DD日 H时m分s秒-----' + moment(now, 'YY年MM月DD日 H时m分s秒'));
console.log('YYYY-MM-DD HH:mm:ss-----' + moment(now, 'YYYY-MM-DD HH:mm:ss'));function add0(n) { return n < 10 ? '0' + n : n }function moment(timestamp, rule) {var time = new Date(timestamp);//将时间戳转为日期var y = time.getFullYear().toString();var mth = time.getMonth() + 1;var d = time.getDate();var h = time.getHours(); //获取时var m = time.getMinutes(); //获取分var s = time.getSeconds(); //获取秒let result = '';if (typeof(rule) === 'undefined') { //未传入规则时的规则:YYYY:MM:DDlet M = mth;M = add0(M)let D = dD = add0(D)result = y + "年" + M + "月" + D + "日"} else { //传入规则//规则1:YY年MM月DD日 H时m分s秒//规则2:YYYY-MM-DD HH:mm:ssresult = rule;if (!/Y{4}/.test(rule)) {result = result.replace(/Y{2}/, y.slice(2, 4))} else {result = result.replace(/Y{4}/, y)}if (!/M{2}/.test(rule)) { //1个Mresult = result.replace(/M/, mth)} else { //2个Mresult = result.replace(/M{2}/, add0(mth))}if (!/D{2}/.test(rule)) { //1个Dresult = result.replace(/D/, d)} else { //2个Dresult = result.replace(/D{2}/, add0(d))}if (!/H{2}/.test(rule)) {result = result.replace(/H/, h)} else {result = result.replace(/H{2}/, add0(h))}if (!/m{2}/.test(rule)) {result = result.replace(/m/, m)} else {result = result.replace(/m{2}/, add0(m))}if (!/s{2}/.test(rule)) { //一个sresult = result.replace(/s/, s)} else {result = result.replace(/s{2}/, add0(s))}}return result
}

小结:
str.replace(reg,s)的使用:
var str = “Visit Microsoft!”
console.log(str.replace(/Microsoft/, “W3School”));
//Visit W3School!

时间戳与特定格式时间转换相关推荐

  1. php date hi,php中时间戳和日期格式的转换

    原文:php中时间戳和日期格式的转换 一,PHP时间戳函数获取指定日期的unix时间戳 strtotime("2009-1-22″) 示例如下: echo strtotime("2 ...

  2. 前端处理方式:特殊格式时间转换(2020-11-27T02:58:41.000000Z)

    前端js处理特殊时间格式(2020-11-27T02:58:41.000000Z): //特殊格式时间转换(2020-11-27T02:58:41.000000Z)function timeChang ...

  3. php用什么服务器系统时间格式,php中时间戳和日期格式的转换

    一,PHP时间戳函数获取指定日期的unix时间戳 strtotime("2009-1-22″) 示例如下: echo strtotime("2009-1-22″) 结果:12325 ...

  4. 时间、时间戳、带格式时间的转换

    一:时间转换成时间戳 //获取时间 let date = new Date(); console.log(date); //输出:Mon Oct 09 2017 14:07:47 GMT+0800 ( ...

  5. QML工作笔记-使用QML中的Date将时间戳和指定格式时间互转

    目录 背景 代码及演示 补充 背景 这个功能非常有用,经常用到,今天查了大半个小时的文档才弄出来,特意记录下,方便以前进行快速查阅,开发. 代码及演示 程序运行截图如下: 源码如下: import Q ...

  6. python时间戳转换成时间_Python时间,日期,时间戳之间转换,时间转换时间戳,Python时间戳转换时间,Python时间转换时间戳...

    #1.将字符串的时间转换为时间戳方法: a = "2013-10-10 23:40:00" #将其转换为时间数组 import time timeArray = time.strp ...

  7. ISO-8601格式时间转换

    /*** @param strTime 示例:"2020-01-01 23:59:59"* @return "2020-01-01T15:59:59Z"*/ / ...

  8. C语言实现字符串格式时间转换时间戳

     1.函数实现 #include <string.h> #include <stdio.h> #include <time.h>/* 判断是否闰年 */ #defi ...

  9. SqlServer时间戳与普通格式的转换

    /********************************************** 时间戳转换(秒) ******************************************* ...

最新文章

  1. 爬虫正则表达式遇到的困难
  2. python四大软件-Python未来可能面临的四大转折
  3. iOS快速集成检查更新
  4. DCT C语言 修改了c程序以及matlab的程序
  5. python 12306 车次数据获取
  6. linux之trap命令
  7. 【Android】Binder机制
  8. Linux字符设备驱动中container_of宏的作用
  9. 文字处理技术:正式开始新布局工作
  10. arXiv引用格式转换为已发表会议标准引用格式小工具分享
  11. 关于PLC的“源型”和“漏型”
  12. 三分钟帮你集成极光推送——和那些你可能不知道的事
  13. A Bluescreen By Any Other Color
  14. centos开启443端口
  15. Android 7.0修改PMS逻辑添加权限白名单
  16. 2021涅普冬令营Misc笔记与题解
  17. html+css实现轮播图
  18. 波段选择方法综述:Hyperspectral Band Selection A review
  19. 【转载】中庸与技术书
  20. ImageJ如何测量物体的面积大小

热门文章

  1. 《深度思维》跨越式*
  2. 轻松获取布局截图 或友盟分享截图
  3. Unity3D笔记——MySQL数据库 简单使用
  4. Voronoi Noise 原理
  5. 代码编辑器 常用注册码
  6. Let‘s Encrypt 安装配置教程
  7. 会声会影 X5 破解版下载 Corel VideoStudio Pro X5
  8. 智能指针手表_智能手表如何检测体内的药物水平
  9. 单边、双边循环快速排序
  10. 【Go语言】实现一个简单的纯后端学员管理系统,入门级别练手项目,练习结构体,接口,构造函数