js 中國標準時間,時間戳 ,yyyy-mm-dd格式之前相互轉換

時間戳 ,yyyy-mm-dd格式都可以通過  new Date()轉換成中國標準時間: Tue Dec 31 2019 00:00:00 GMT+0800 (中國標準時間)

new Date()的參數可以爲空,可以爲:

1、new Date("month dd,yyyy hh:mm:ss"); 

2、new Date("month dd,yyyy"); 

3、new Date(yyyy,mth,dd,hh,mm,ss); 注意:這種方式下,必須傳遞整型

4、new Date(yyyy,mth,dd); 

5、new Date(ms); 注意:ms:是需要創建的時間和 GMT時間1970年1月1日之間相差的毫秒數;當前時間與GMT1970.1.1之間的毫秒數:var mills = new Date().getTime();

var standardDate = new Date('2018,9,1');
console.log(standardDate);//Wed Jan 30 2019 15:48:29 GMT+0800 (中國標準時間)

var standardDay = standardDate.getTime(standardDate);
console.log(standardDay);//1548834600929

//只對(中國標準時間)起作用,formatting得到yyyy-mm-dd格式的日期
var year = standardDate.getFullYear();
console.log(year);//2019
var month= standardDate.getMonth()+1;
console.log(month);//1
var day = standardDate.getDate();
console.log(day);//30
var hour = standardDate.getHours();
var minute = standardDate.getMinutes();
var second = standardDate.getSeconds();
var formatting = year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second;
console.log(formatting);//2018-9-1 0:0:0

yyyy-mm-dd格式日期計算時間間隔,單位爲天

timeLimit(BeginDate,EndDate){
    let begin = new Date(BeginDate);
    let end = new Date(EndDate);
    let beginDate = begin.getTime(begin);
    let endDate = end.getTime(end);
    var days=Math.floor((endDate-beginDate)/(24*3600*1000))
    return days;
}

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章