js时间戳转日期方法
AI 概述
js 时间戳转日期方法
/**
* [formatDateTime 时间戳转年月日 时分秒]
* @param {[type]} int [时间戳]
* @param {[type]} str [时间格式,格式如下]
* 时间格式,[y-m-d h:i:s , y-m-d , h:i:s , ymdhis , y-m , m-d , y 年 m 月 d 日 , y 年 m 月 , m 月 d 日 , y 年 m 月 d 日 h 时 i 分 s 秒 , h 时 i 分...
js 时间戳转日期方法
/**
* [formatDateTime 时间戳转年月日 时分秒]
* @param {[type]} int [时间戳]
* @param {[type]} str [时间格式,格式如下]
* 时间格式,[y-m-d h:i:s , y-m-d , h:i:s , ymdhis , y-m , m-d , y 年 m 月 d 日 , y 年 m 月 , m 月 d 日 , y 年 m 月 d 日 h 时 i 分 s 秒 , h 时 i 分 s 秒]
* @param {Boolean} bol [默认为 true ,表示 10 以下前面补零]
* @return {[type]} [返回对应格式的日期时间格式]
*/
function formatDateTime(int, str, bol = true) {
let date = new Date(int);
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
let hours = date.getHours();
let minutes = date.getMinutes();
let seconds = date.getSeconds();
if (bol) {
month = month < 10 ? '0' + month : month
day = day < 10 ? '0' + day : day
hours = hours < 10 ? '0' + hours : hours
minutes = minutes < 10 ? '0' + minutes : minutes
seconds = seconds < 10 ? '0' + seconds : seconds
}
let strtoLowerCase = str.toLowerCase(); //把传入的转换成小写
console.log(strtoLowerCase)
switch (strtoLowerCase) {
case 'y-m-d h:i:s':
return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
break;
case 'ymdhis':
return year + '' + month + '' + day + '' + hours + '' + minutes + '' + seconds;
break;
case 'y-m-d':
return year + '-' + month + '-' + day;
break;
case 'y-m':
return year + '-' + month;
break;
case 'm-d':
return month + '-' + day;
break;
case 'ymd':
return year + '' + month + '' + day;
break;
case 'h:i:s':
return hours + ':' + minutes + ':' + seconds;
break;
case 'his':
return hours + '' + minutes + '' + seconds;
break;
case 'y 年 m 月 d 日 h 时 i 分 s 秒':
return year + '年' + month + '月' + day + '日' + ' ' + hours + '时' + minutes + '分' + seconds + '秒';
break;
case 'y 年 m 月 d 日':
return year + '年' + month + '月' + day + '日';
break;
case 'h 时 i 分 s 秒':
return hours + '时' + minutes + '分' + seconds + '秒';
break;
case 'y 年 m 月':
return year + '年' + month + '月';
break;
case 'm 月 d 日':
return month + '月' + day + '日';
break;
}
}
document.write("时间戳: "+new Date());
document.write("<br>");
document.write("转换结果: "+formatDateTime(new Date(), 'y 年 m 月 d 日', bol = true))
以上关于js时间戳转日期方法的文章就介绍到这了,更多相关内容请搜索码云笔记以前的文章或继续浏览下面的相关文章,希望大家以后多多支持码云笔记。
声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 admin@mybj123.com 进行投诉反馈,一经查实,立即处理!
重要:如软件存在付费、会员、充值等,均属软件开发者或所属公司行为,与本站无关,网友需自行判断
码云笔记 » js时间戳转日期方法
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 admin@mybj123.com 进行投诉反馈,一经查实,立即处理!
重要:如软件存在付费、会员、充值等,均属软件开发者或所属公司行为,与本站无关,网友需自行判断
码云笔记 » js时间戳转日期方法
微信
支付宝