時間戳轉換為各種時間制
阿新 • • 發佈:2019-01-05
- //時間戳轉換成日期時間2014-8-8 下午11:40:20
- function formatDate(ns){
- return new Date(parseInt(ns) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
- }
- //時間戳轉換成八位日期2014-5-5
- function userDate(uData){
- var myDate = new Date(uData*1000);
- var year = myDate.getFullYear();
-
var month = myDate.getMonth() + 1;
- var day = myDate.getDate();
- return year + '-' + month + '-' + day;
- }
- //時間戳轉換成四位時間10:10
- function userTime(uTime){
- var myDate = new Date(uTime*1000);
- var hours = myDate.getHours();
- var minutes = myDate.getMinutes();
- return hours + ':' + minutes;
- }
-
//時間戳轉換成四位時間10:10:00
- function userTime(uTime){
- var myDate = new Date(uTime*1000);
- var hours = myDate.getHours();
- var minutes = myDate.getMinutes();
- var second = myDate.getSeconds();
- return hours + ':' + minutes + ':' + second;
- }
- //定時提醒設定的時間傳入 (2014,05,15)返回成2014-01-21
-
function setDate(year,month,day){
- return year + '-' + month + '-' + day;
- }
- //定時提醒設定的時間傳入 (01:02)返回成01:01:00
- function setTime(hour,minute){
- return hour + ':' + minute+ ':00';
- }
- //時間格式2014-02-02 14:10:00改成時間戳
- function js_strto_time(str_time){
- var new_str = str_time.replace(/:/g,"-");
- new_str = new_str.replace(/ /g,"-");
- var arr = new_str.split("-");
- var datum = new Date(Date.UTC(arr[0],arr[1]-1,arr[2],arr[3]-8,arr[4],arr[5]));
- return strtotime = datum.getTime()/1000;
- }
- //時間戳改成時間格式2014-12-12 下午01:10
- function js_date_time(unixtime){
- var timestr = new Date(parseInt(unixtime) * 1000);
- var datetime = timestr.toLocaleString().replace(/年|月/g,"-").replace(/日/g," ");
- return datetime;
- }
又有個時間格式:2014-1-1 10:10 轉換成時間戳
Js程式碼- function js_strto_times(str_time){
- var new_str = str_time.replace(/:/g,"-");
- new_str = new_str.replace(/ /g,"-");
- var arr = new_str.split("-");
- var datum = new Date(Date.UTC(arr[0],arr[1]-1,arr[2],arr[3]-8,arr[4]));
- return strtotime = datum.getTime()/1000;
- }