Jsでの日付の使用

1.今日の0:0:0を取得します(通常、開始日を取得するために使用されます)

var startDate = new Date(new Date()。toLocaleDateString()); // 2018年5月15日火曜日00:00:00 GMT + 0800(中国標準時)
 

2. 1か月前の日付を取得します

var lastM = new Date(new Date()。setMonth(new Date()。getMonth()-1)); // 2018年4月15日09:18:08 GMT + 0800(中国標準時)
 

3. 0を取得:0:1か月前

var lastM_start = new Date(new Date(new Date()。toLocaleDateString())。setMonth(new Date()。getMonth()-1));
// 2018年4月15日00:00:00 GMT + 0800(中国標準時間)
 

4.前日の日付を取得する

var yesterday = new Date(new Date()。setDate(new Date()。getDate()-1)); // 2018年5月14日09:26:39 GMT + 0800(中国標準時)
 

5.今日の23:59:59を取得

var endDate = new Date(new Date(new Date()。toLocaleDateString())。getTime()+ 24 * 60 * 60 * 1000-1);
// 2018年5月15日火曜日23:59:59 GMT + 0800(中国標準時間) 
 

6.昨日の23:59:59を取得

var yes_endDate = new Date(new Date(new Date(
new Date()。setDate(new Date()。getDate()-1))。toLocaleDateString())。getTime()+ 24 * 60 * 60 * 1000-1 );
// 2018年5月14日月曜日23:59:59 GMT + 0800(中国标准时间)

 

https://www.w3school.com.cn/jsref/jsref_obj_date.asp

 

 

例:

入力時刻の後の日付を取得し、日付文字列を返します

let endTime = '2020-01-01';
let endTime = formatDate(new Date(new Date(endTime).setDate(new Date(endTime).getDate()+1)));
console.log(endTime);//2020-01-02


//返回格式化日期 yyyy-MM-dd
function formatDate(date){
    return PrefixInteger(date.getFullYear(),4)+"-"+PrefixInteger(parseInt(date.getMonth()+1),2) +"-"+PrefixInteger(date.getDate(),2);
}
//数字转字符串,前方自动补零
function PrefixInteger(num, n) {
    return (Array(n).join(0) + num).slice(-n);
}

 

元の記事83件を公開 38のような 230,000以上を訪問

おすすめ

転載: blog.csdn.net/mao_mao37/article/details/105144629