当前位置:网站首页>2022-06-10 work record --js- obtain the date n days after a certain date

2022-06-10 work record --js- obtain the date n days after a certain date

2022-06-24 22:37:00 Little radish

JS- Get to a certain date N Days after

If I have got a date , Then I want to add 7 God :

  • Implementation method : Convert the currently obtained date to Time stamp , then add 7*24*3600, If it is millisecond ( The time stamp is 13 position ) If you want to 3600 Back *1000, The results obtained are converted into date .【 This method is provided by the company's excellent brother , Like it by hand 】

Conversion between date and timestamp Check out my other blog :https://blog.csdn.net/weixin_48850734/article/details/124668189
 Insert picture description here

Get to a certain date 7 Days after ️

//  Convert date format to timestamp 
function timeToTimestamp(time) {
    
	return new Date(time).getTime();
}

//  Convert timestamps to date format 
function timestampToTime(timestamp) {
     
    let d = new Date((timestamp+'').length==10?timestamp*1000:timestamp);//  Timestamp 10 Bitwise demand *1000, Timestamp 13 You don't need to take a seat 1000【(timestamp+'') Express : Convert to string 】
    let yyyy = d.getFullYear() + '-'; //  year 
    let MM = (d.getMonth() < 10 ? '0'+(d.getMonth()+1) : (d.getMonth())+1) + '-'; //  month  (js Get the month in date.getMonth() What you get is 0-11, So we need to +1)
    let dd = (d.getDate() < 10 ? '0'+d.getDate() : d.getDate()); //  Japan 
    let h = (d.getHours() < 10 ? '0'+d.getHours() : d.getHours()) + ':'; //  Hours 
    let m = (d.getMinutes() < 10 ? '0'+d.getMinutes() : d.getMinutes()) + ':'; //  minute 
    let s = (d.getSeconds() < 10 ? '0'+d.getSeconds() : d.getSeconds()); //  second 
    return yyyy + MM + dd + ' ' + h + m + s;
}

//  Convert the currently acquired date into a timestamp 
let curr_timestamp = timeToTimestamp('2022-06-22 14:00:00'); // 1655877600000
//  obtain 7 Days after 
timestampToTime(curr_timestamp+7*24*3600*1000); // '2022-06-29 14:00:00'
原网站

版权声明
本文为[Little radish]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241632082384.html