时间戳与特定格式时间转换
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!
时间戳与特定格式时间转换相关推荐
- php date hi,php中时间戳和日期格式的转换
原文:php中时间戳和日期格式的转换 一,PHP时间戳函数获取指定日期的unix时间戳 strtotime("2009-1-22″) 示例如下: echo strtotime("2 ...
- 前端处理方式:特殊格式时间转换(2020-11-27T02:58:41.000000Z)
前端js处理特殊时间格式(2020-11-27T02:58:41.000000Z): //特殊格式时间转换(2020-11-27T02:58:41.000000Z)function timeChang ...
- php用什么服务器系统时间格式,php中时间戳和日期格式的转换
一,PHP时间戳函数获取指定日期的unix时间戳 strtotime("2009-1-22″) 示例如下: echo strtotime("2009-1-22″) 结果:12325 ...
- 时间、时间戳、带格式时间的转换
一:时间转换成时间戳 //获取时间 let date = new Date(); console.log(date); //输出:Mon Oct 09 2017 14:07:47 GMT+0800 ( ...
- QML工作笔记-使用QML中的Date将时间戳和指定格式时间互转
目录 背景 代码及演示 补充 背景 这个功能非常有用,经常用到,今天查了大半个小时的文档才弄出来,特意记录下,方便以前进行快速查阅,开发. 代码及演示 程序运行截图如下: 源码如下: import Q ...
- python时间戳转换成时间_Python时间,日期,时间戳之间转换,时间转换时间戳,Python时间戳转换时间,Python时间转换时间戳...
#1.将字符串的时间转换为时间戳方法: a = "2013-10-10 23:40:00" #将其转换为时间数组 import time timeArray = time.strp ...
- ISO-8601格式时间转换
/*** @param strTime 示例:"2020-01-01 23:59:59"* @return "2020-01-01T15:59:59Z"*/ / ...
- C语言实现字符串格式时间转换时间戳
1.函数实现 #include <string.h> #include <stdio.h> #include <time.h>/* 判断是否闰年 */ #defi ...
- SqlServer时间戳与普通格式的转换
/********************************************** 时间戳转换(秒) ******************************************* ...
最新文章
- 爬虫正则表达式遇到的困难
- python四大软件-Python未来可能面临的四大转折
- iOS快速集成检查更新
- DCT C语言 修改了c程序以及matlab的程序
- python 12306 车次数据获取
- linux之trap命令
- 【Android】Binder机制
- Linux字符设备驱动中container_of宏的作用
- 文字处理技术:正式开始新布局工作
- arXiv引用格式转换为已发表会议标准引用格式小工具分享
- 关于PLC的“源型”和“漏型”
- 三分钟帮你集成极光推送——和那些你可能不知道的事
- A Bluescreen By Any Other Color
- centos开启443端口
- Android 7.0修改PMS逻辑添加权限白名单
- 2021涅普冬令营Misc笔记与题解
- html+css实现轮播图
- 波段选择方法综述:Hyperspectral Band Selection A review
- 【转载】中庸与技术书
- ImageJ如何测量物体的面积大小