博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JS中将时间转为指定格式
阅读量:3924 次
发布时间:2019-05-23

本文共 973 字,大约阅读时间需要 3 分钟。

var format = function(time, format){
var t = new Date(time); var tf = function(i){
return (i < 10 ? '0' : '') + i}; return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a){
switch(a){
case 'yyyy': return tf(t.getFullYear()); break; case 'MM': return tf(t.getMonth() + 1); break; case 'mm': return tf(t.getMinutes()); break; case 'dd': return tf(t.getDate()); break; case 'HH': return tf(t.getHours()); break; case 'ss': return tf(t.getSeconds()); break; } })}alert(format(new Date().getTime(), 'yyyy-MM-dd HH:mm:ss')//new Date().getTime() 获取时间毫秒数
//IOS new Date() 中的时间不能用(2021-06-30 17:30) 这种格式.否则会报错(不能用 '-')//解决方法:使用正则转换一下var str = '2021-06-30 17:30'new Date(str.replace(/-/g,'/'));

转载地址:http://lgkgn.baihongyu.com/

你可能感兴趣的文章
Linux实操--实用指令Day3
查看>>
spring+springboot认识
查看>>
Leetcode 136. 只出现一次的数字
查看>>
Leetcode 11. 盛最多水的容器
查看>>
Leetcode 121. 买卖股票的最佳时机
查看>>
Leetcode 123. 买卖股票的最佳时机 III
查看>>
Leetcode 24. 两两交换链表中的节点
查看>>
Leetcode 100. 相同的树
查看>>
Leetcode 101. 对称二叉树
查看>>
Leetcode 108. 将有序数组转换为二叉搜索树
查看>>
Leetcode 303. 区域和检索 - 数组不可变
查看>>
Leetcode 110. 平衡二叉树
查看>>
Leetcode 111. 二叉树的最小深度
查看>>
Leetcode 226. 翻转二叉树
查看>>
Leetcode 617. 合并二叉树
查看>>
Leetcode 654. 最大二叉树
查看>>
Leetcode 304. 二维区域和检索 - 矩阵不可变
查看>>
Leetcode 45. 跳跃游戏 II
查看>>
模式2. 工厂方法模式-Java
查看>>
模式1. 简单工厂模式-Java
查看>>