js时间格式化
2016-09-18 15:28
471 查看
const formatDate = timestamp => {
const date = new Date(timestamp);
const m = date.getMonth() + 1;
const d = date.getDate();
const h = date.getHours();
const i = date.getMinutes();
return m + '月' + d + '日' + ' ' + h + ':' + i;
};
使用 :
formatDate(startTime * 1000)
/**
* 日期时间格式化 (CST 转化为标准时间可用)
*/
exports.formatDateCst = function(stamp, formatString){
var date = new Date(stamp);
var o = {
'M+': date.getMonth() + 1,
'D+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds(),
'S' : date.getMilliseconds()
};
if(/(Y+)/.test(formatString)){
formatString = formatString.replace(RegExp.$1, (date.getFullYear() + "").substr(2 - RegExp.$1.length));
}
for(var k in o){
if(new RegExp('(' + k + ')').test(formatString)){
formatString = formatString.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
}
}
return formatString;
}
use: formatDateCst(value,'YY-MM-DD hh:mm:ss')
const date = new Date(timestamp);
const m = date.getMonth() + 1;
const d = date.getDate();
const h = date.getHours();
const i = date.getMinutes();
return m + '月' + d + '日' + ' ' + h + ':' + i;
};
使用 :
formatDate(startTime * 1000)
/**
* 日期时间格式化 (CST 转化为标准时间可用)
*/
exports.formatDateCst = function(stamp, formatString){
var date = new Date(stamp);
var o = {
'M+': date.getMonth() + 1,
'D+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds(),
'S' : date.getMilliseconds()
};
if(/(Y+)/.test(formatString)){
formatString = formatString.replace(RegExp.$1, (date.getFullYear() + "").substr(2 - RegExp.$1.length));
}
for(var k in o){
if(new RegExp('(' + k + ')').test(formatString)){
formatString = formatString.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
}
}
return formatString;
}
use: formatDateCst(value,'YY-MM-DD hh:mm:ss')
相关文章推荐
- radio js toString bindable disabled evevt.srcElement. 多个空格变成一个空格 js格式化时间 [Bindable(true)] jquery img select
- js 格式化时间日期函数小结
- [JS代码]时间对象的格式化 (转自MEIZZ(梅花雪)的BLOG)
- JS获得当前时间 并格式化为:yyyy-MM-dd HH:MM:SS
- js时间格式化
- 自动格式化时间js
- js中unix时间格式化为时分秒格式
- js格式化时间
- js时间戳格式化成日期格式函数
- js中格式化时间字符串
- js 格式化时间
- JS获取系统当前时间并格式化输出小函数
- js中格式化时间
- js 格式化时间
- js 格式化时间函数
- js时间格式化工具类【转载】
- 杂记(DataTable筛选,时间格式化,JS验证数字,JS求和)
- JS时间格式化(短日期)
- js时间格式化工具类
- js中格式化时间字符串