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); ...
随机推荐
- 一步一步教你玩转.NET Framework的配置文件app.config
转自https://www.cnblogs.com/tonnie/archive/2010/12/17/appconfig.html 在一般的项目中,为了使你的代码更加灵活,更方便调整,减少不必要的h ...
- SqlServer自定义排序
在实际项目中,有时会碰到数据库SQL的特殊排序需求,举几个例子,作为参考. 1.自定义优先级 一种常见的排序需求是指定某个字段取值的优先级,根据指定的优先级展示排序结果.比如如下表: Create T ...
- asp页面无法访问,可尝试开始SQL Server等服务
存在问题 asp页面的英文提示,翻译后为: "一个错误发生在服务器在处理URL.请联系系统管理员(管理人).如果您是系统管理员,请点击这里了解更多关于这个错误." 解决方案 请 ...
- cv2.minAreaRect() 生成最小外接矩形
简介 使用python opencv返回点集cnt的最小外接矩形,所用函数为 cv2.minAreaRect(cnt) ,cnt是所要求最小外接矩形的点集数组或向量,这个点集不定个数. cv2 ...
- MySQL——数据库和 SQL 概念&&MySQL的安装
数据库和 SQL 概念 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,它的产生距今已有六十多年.随着信息技术和市场的发展,数据库变得无处不在:它在电子商务.银行系统等众多领域都 ...
- 用户输入和while循环
函数input()的工作原理 message=input('Tell me something,and I will repeat it back to you:') print(message) 编 ...
- SCOPE_IDENTITY和@@IDENTITY[转]
本文转自:http://www.cnblogs.com/daydayupanan/archive/2008/09/04/1283648.html SCOPE_IDENTITY和@@IDENTITY的作 ...
- Java第7次作业:造人类(用private封装,用static关键字自己造重载输出方法)什么是面向对象程序设计?什么是类和对象?什么是无参有参构造方法 ?什么是封装?
什么是面向对象程序设计? 我们称为OOP(Object Oriented Programming) 就是非结构化的程序设计 要使用类和对象的方法来进行编程 什么是类,什么是对象 类就是封装了属性和 ...
- cocos2dx lua 打印和保存日志
在2d游戏中,经常会出现闪退或者报错的问题,通过写文本,将日志文件发送给服务端,让后端人员进行分析. 通过lua打印日志在文本文件中: local file = io.open(cc.FileUtil ...
- NSRegularExpression
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAu4AAAU2CAIAAABFtaRRAAAAAXNSR0IArs4c6QAAAe9pVFh0WE1MOm