PHP :比較簡單
$str = 'Wed Jul 24 11:24:33 CST 2019'; echo date('Y-m-d H:i:s', strtotime($str)); echo date('Y-m-d H:i:s',strtotime("$date1 -14 hours"));
PHP 直接格式化的時間相差14個小時,然后我又減去了14個小時,
JavaScript:好復雜的感覺
dateFormat = function (date, format) { date = new Date(date); var o = { 'M+' : date.getMonth() + 1, //month 'd+' : date.getDate(), //day 'H+' : date.getHours(), //hour 'm+' : date.getMinutes(), //minute 's+' : date.getSeconds(), //second 'q+' : Math.floor((date.getMonth() + 3) / 3), //quarter 'S' : date.getMilliseconds() //millisecond }; if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp('(' + k + ')').test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)); return format; } var _xbdate = new Date(dateFormat('Wed Jul 24 11:24:33 CST 2019','yyyy-MM-dd HH:mm:ss')); //將CST時間轉換為GMT格式
console.log(_xbdate);
nowDate = new Date(_xbdate.valueOf() - 60* 60 * 1000*14);// 當前時間減去14小時
console.log(nowDate);
var year = nowDate.getFullYear(); var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1): nowDate.getMonth() + 1; var day = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate(); var hours = nowDate.getHours()<10?"0" + nowDate.getHours():nowDate.getHours(); var minutes = nowDate.getMinutes()<10?"0" + nowDate.getMinutes():nowDate.getMinutes(); var seconds = nowDate.getSeconds()<10?"0" + nowDate.getSeconds():nowDate.getSeconds(); var dateStr = year + "-" + month + "-" + day+ " " + hours+ ":" + minutes+ ":" + seconds; //轉為YY-mm-dd H:i:s console.log(dateStr);
運行結果
真麻煩啊,PHP是世界上最好的語言!!!
但是js確實強大啊
小科普:中央標准時間(CST)
CST可視為美國、澳大利亞、古巴或中國的標准時間。
CST可以為如下4個不同的時區的縮寫:
美國中部時間:Central Standard Time (USA) UT-6:00
澳大利亞中部時間:Central Standard Time (Australia) UT+9:30
中國標准時間:China Standard Time UT+8:00
古巴標准時間:Cuba Standard Time UT-4:00