js时间格式化工具,时间戳格式化,字符串转时间戳
在开发中经常会用到时间格式化,有时候在网上搜索一大堆但不是自己想要的,自己总结一下,写一个时间格式化工具方便以后直接使用,欢迎大家来吐槽……
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
/** * Created by linxins on 2016/6/16. */ if ( typeof linxins !== 'function' ) { var linxins = function (){}; } ( function (){ var _self = this .linxins; /** * 获取当前时间的格式化日期 * @param string Fmt eg:Y-m-d H:i:s * @param boolean hasZero eg:true|false * @returns {string} */ _self.dateFormat = function (Fmt, hasZero){ return timeFormat(Fmt, hasZero); } /** * 将时间戳格式化 * @param number timestamp eg:1465963958000 length:13 * @param string Fmt eg:Y-m-d H:i:s * @param boolean hasZero eg:true|false * @returns {string} */ return timeFormat(timestamp, Fmt, hasZero); } /** * 时间字符串转时间戳 * @param string dateStr eg:2016-06-16 16:15:59 * @returns {number} */ _self.dateStr2timestamp = function (dateStr){ return ( typeof dateStr === 'string' ) ? Date.parse( new Date(dateStr)) : Date.parse( new Date()); } /** * 将时间戳格式化 * @param number timestamp eg:1465963958000 length:13 * @param string Fmt eg:Y-m-d H:i:s * @param boolean hasZero eg:true|false * @returns {string} */ function timeFormat(timestamp, Fmt, hasZero){ var date = ( typeof timestamp != 'undefined' && timestamp != '' ) ? new Date(timestamp) : new Date(); var hasZero = ( typeof hasZero === 'boolean' ) ? hasZero : true ; var Y = date.getFullYear(); var m = (hasZero && date.getMonth()+1 < 10) ? '0' +(date.getMonth()+1) : date.getMonth()+1; var d = (hasZero && date.getDate() < 10) ? '0' +date.getDate() : date.getDate(); var H = (hasZero && date.getHours() < 10) ? '0' +date.getHours() : date.getHours(); var i = (hasZero && date.getMinutes() < 10) ? '0' +date.getMinutes() : date.getMinutes(); var s = (hasZero && date.getSeconds() < 10) ? '0' +date.getSeconds() : date.getSeconds(); var fomateTime = '' ; switch (Fmt){ case 'YmdHis' : fomateTime = Y+m+d+H+i+s; break ; case 'Y-m-d H:i:s' : fomateTime = Y+ '-' +m+ '-' +d+ ' ' +H+ ':' +i+ ':' +s; break ; case 'Y/m/d H:i:s' : fomateTime = Y+ '/' +m+ '/' +d+ ' ' +H+ ':' +i+ ':' +s; break ; case 'Y-m-d H:i' : fomateTime = Y+ '-' +m+ '-' +d+ ' ' +H+ ':' +i; break ; case 'Y-m-d H' : fomateTime = Y+ '-' +m+ '-' +d+ ' ' +H; break ; case 'Y-m-d' : fomateTime = Y+ '-' +m+ '-' +d; break ; case 'Ymd' : fomateTime = Y + m + d; break ; case 'H:i:s' : fomateTime = H+ ':' +i+ ':' +s; break ; default : fomateTime = Y+ '-' +m+ '-' +d+ ' ' +H+ ':' +i+ ':' +s; break ; } return fomateTime; } })(window); |
//测试datetimeUtil
console.log(linxins.dateFormat());//当前时间格式:2016-06-16 16:44:49
console.log(linxins.dateStr2timestamp('2016-06-15 12:12:38'));//1465963958000
console.log(linxins.timestampFormat(1465963958000, 'Y/m/d H:i:s', false));//
js时间格式化工具,时间戳格式化,字符串转时间戳的更多相关文章
- js获取时间和日期,字符串和时间戳之间的转换
//获取当前时间: var myDate = new Date();//当前时间 var year = myDate.getFullYear();//当前年份 var month = myDate.g ...
- 调用get_str_time(时间), 就能把毫秒的时间转换成格式化的 ,转化时间戳的方法
function get_str_time(time){ var datetime = new Date(); datetime.setTime(time); var year = datetime. ...
- JS时间格式转成字符串
formatNumber = n => { n = n.toString(); return n[1] ? n : '0' + n }; // 时间格式化 formatTime = date = ...
- js 时间格式化工具类
/** * 返回示例:0 天 4 小时 7 分钟 57 秒 * @param second 毫秒数 * @returns {String} 时间html */ function secondToDay ...
- MySQL 日期、字符串、时间戳互转
背景 原文地址:https://www.cnblogs.com/jhy-ocean/p/5560857.html 平时比较常用的时间.字符串.时间戳之间的互相转换,虽然常用但是几乎每次使用时候都喜欢去 ...
- js 时间格式转换
js时间格式转换 格式化时间转成时间戳 //格式化转时间戳(单位秒) function strtotime(strtime) { strtime = strtime.substring(0, 19); ...
- 时间戳显示为多少分钟前,多少天前的JS处理,JS时间格式化,时间戳的转换
var dateDiff = function (timestamp) { // 补全为13位 var arrTimestamp = (timestamp + '').split(''); for ( ...
- js时间格式化函数,支持Unix时间戳
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- JS 时间字符串与时间戳之间的转换
1.当前时间换时间戳 var timestamp = parseInt(new Date().getTime()/1000); // 当前时间戳 document.write(timestamp); ...
随机推荐
- linux mount命令详解(iso文件挂载)
挂载命令: mount [-t vfstype] [-o options] device dir mount 是挂载命令 -t + 类型 -o + 属性 device iso的文件 dir 挂 ...
- 升级CentOS内核 - 2.6升级到3.10/最新内核
##记得切换到root用户执行升级操作. [root@localhost ~]# uname -a ##旧版 Linux localhost.localdomain 2.6.32-279.el6.i6 ...
- sql问题:备份集中的数据库备份与现有的 '办公系统' 数据库不同
解决方法:把备份的数据库从原有的地方先分离,再拷贝一份,在需要还原的服务器上附加到数据库中,在根数据库上点击“还原数据库”,选择需要还原的数据库名称,以及还原的bak备份文件,在选择“选项”,勾选上“ ...
- winform ListView创建columnHeader的方法
using System; using System.Windows.Forms; using System.Drawing; using System.Collections; namespace ...
- LINQ 基础语句
去全部集合 using (dat0216DataContext con = new dat0216DataContext()) { //LoList 是转换成 List集合 List<Us ...
- python基础教程总结15——4 新闻聚合
NNTP:网络新闻传输协议,Network News Transfer Protocol 目标: 从多种不同的来源收集新闻: 用户可以轻松添加新的新闻来源(甚至是新类型的新闻来源: 程序可以将编译好的 ...
- C#链接mysql 新手容易出错的问题
1.Access denied for user 'root'@'DESKTOP-AN72KEI' (using password: YES) 出现这个问题的原因是因为mysql的自带用户root理论 ...
- Codeforces Round #321 (Div. 2) D Kefa and Dishes(dp)
用spfa,和dp是一样的.转移只和最后一个吃的dish和吃了哪些有关. 把松弛改成变长.因为是DAG,所以一定没环.操作最多有84934656,514ms跑过,实际远远没这么多. 脑补过一下费用流, ...
- python之函数默认参数的坑
坑 当你的默认参数如果是可变的数据类型,你要小心了 例题 # 正常没毛病的操作 def func(a,b=False): print(a) print(b) func(1,True) # 在实参角度, ...
- datatable css not work
样式不显示问题 无论是放内联样式文件还是直接放HTML文件都不显示 后来发现是因为datatable是放在后面初始化,它自带的样式覆盖了我们自定义的样式 所以要注意写code时,很多时候不是code不 ...