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); ...
随机推荐
- Android安卓电话拦截及短信过滤
package com.focus.manager; import java.lang.reflect.Method; import Android .app.Activity; import And ...
- for循环/计算坐标
for循环计算坐标 webqq里面有类似桌面的各种图标,是绝对定位的,这样可以拖动改变位置,用浮动的话,没法拖动. <!DOCTYPE html> <html lang=" ...
- EasyUI:Easyui parser的用法
Easyui的渲染机制是个比较坑的事情,在项目开发中,遇到需要等其渲染完成后处理一些事情,比如为联动的下拉框选中默认值,为某些表单元素自动填充值等!这就需要用到Easyui parser解析器了.官方 ...
- document.all.item作用
1.document.all.myCheckBox和 document.all.item通过控件的名字定位控件,item()中是控件的名字例如:<input type="checkbo ...
- vue.js的package.json相关问题解惑
使用vue-cli创建vue.webpack项目,在项目中用到了iSlider移动端滑动插件,只在本地命令工具中npm install islider.js:提交之后,partner下载代码后一直运行 ...
- 如何用JavaScript实现2+2=5?
我大学毕业找工作时,经常做一些稀奇古怪的面试题.这不,给大家分享一道整蛊的面试题,它其实不能算一道正式的面试题,大家可以用它来捉弄你们那些程序员朋友. 题目:如何用JavaScript实现2+2=5? ...
- [uva816]AbbottsRevenge Abbott的复仇(经典迷宫BFS)
这题思路就普通的BFS加上一个维度朝向,主要是要注意输入,输出,以及细节的处理 #include<cstdio> #include<cstring> #include<q ...
- Core Data stack
https://developer.apple.com/library/content/documentation/DataManagement/Devpedia-CoreData/coreDataS ...
- 《剑指offer》39题—数组中出现次数超过一半的数字
题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...
- Python中文编码问题(字符串前面加'u')
中文编码问题是用中文的程序员经常头大的问题,在python下也是如此,那么应该怎么理解和解决python的编码问题呢? 我们要知道python内部使用的是unicode编码,而外部却要面对千奇百怪的各 ...