js有关时间换算的一些方法
生活随笔
收集整理的這篇文章主要介紹了
js有关时间换算的一些方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
工作中常常會遇到后臺返回的值是毫秒,這時候就需要前端根據需要轉換一下時間格式;
1、換算年月日
function timeFormat(date) {var format = 'yyyy-MM-dd';var t = new Date(date);var tf = function(i) {return (i < 10 ? '0' : '') + i};var time = format.replace(/yyyy|MM|dd/g, function(a) {switch (a) {case 'yyyy':return tf(t.getFullYear());break;case 'MM':return tf(t.getMonth() + 1);break;case 'dd':return tf(t.getDate());break;}})return time;} 2、換算年月日 時分秒 function timeFormatT(date) {var format = 'yyyy-MM-dd hh:mm:ss';var t = new Date(date);var tf = function(i) {return (i < 10 ? '0' : '') + i};var time = format.replace(/yyyy|MM|dd|hh|mm|ss/g, function(a) {switch (a) {case 'yyyy':return tf(t.getFullYear());break;case 'MM':return tf(t.getMonth() + 1);break;case 'dd':return tf(t.getDate());break;case 'hh':return tf(t.getHours());break;case 'mm':return tf(t.getMinutes());break;case 'ss':return tf(t.getSeconds());break;}})return time;}3、時間的前后推移
function GetDateStr(AddDayCount) {var dd = new Date();dd.setDate(dd.getDate()+AddDayCount);var y = dd.getFullYear();var m = (dd.getMonth()+1)<10?"0"+(dd.getMonth()+1):(dd.getMonth()+1);var d = dd.getDate()<10?"0"+dd.getDate():dd.getDate();return y+"-"+m+"-"+d;}GetDateStr(30)//往前推30天GetDateStr(-30)//往后推30天?
?
總結
以上是生活随笔為你收集整理的js有关时间换算的一些方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt FFmpeg 音视频播放器
- 下一篇: git修改commit信息