現在の時刻を取得し、指定日の前日、翌日、前日の日付、指定時間の前、後n分の日付を指定します.
41156 ワード
現在の時刻を取得し、前の日、後の日、前のn日間の日付を指定します.
module.exports = {
formatTime: formatTime,
getNextOrPreDate:getNextOrPreDate,
getBeforeDate: getBeforeDate,
getNowDate: getNowDate,
getBeforeDay: getBeforeDay,
getLastDay: getLastDay
}
function add0(m) {
return m < 10 ? '0' + m : m
}
//
function formatTime(shijianchuo) {
//shijianchuo , parseInt
var time = new Date(shijianchuo * 1000);
var y = time.getFullYear();
var m = time.getMonth() + 1;
var d = time.getDate();
var h = time.getHours();
var mm = time.getMinutes();
var s = time.getSeconds();
return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
}
// , YYYY-MM-DD
function getNowDate() {
var date = new Date();
var seperator1 = "-";
var year = date.getFullYear();
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = '0'+month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = '0'+strDate;
}
var currentdate = year + seperator1 + month + seperator1 + strDate;
return currentdate;
}
// 、 ,-1 ,1
function getNextOrPreDate(date, day) {
var dd = new Date(date);
dd.setDate(dd.getDate() + day);
var y = dd.getFullYear();
var m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
return y + "-" + m + "-" + d;
}
//
function getBeforeDay(d) {
var d = new Date(d);
console.log('d',d)
d = +d - 1000 * 60 * 60 * 24;
d = new Date(d);
console.log('dddddd', d)
var year = d.getFullYear();
console.log(year)
var mon = d.getMonth() + 1;
console.log(mon)
var day = d.getDate();
console.log(day)
var s = year + "-" + (mon < 10 ? ('0' + mon) : mon) + "-" + (day < 10 ? ('0' + day) : day);
console.log(s)
return s;
}
//
function getLastDay(d) {
d = new Date(d);
d = +d + 1000 * 60 * 60 * 24;
d = new Date(d);
var year = d.getFullYear();
var mon = d.getMonth() + 1;
var day = d.getDate();
var s = year + "-" + (mon < 10 ? ('0' + mon) : mon) + "-" + (day < 10 ? ('0' + day) : day);
return s;
}
// n
function getBeforeDate(n) {
var n = n;
var d = new Date();
var year = d.getFullYear();
var mon = d.getMonth() + 1;
var day = d.getDate();
if (day <= n) {
if (mon > 1) {
mon = mon - 1;
} else {
year = year - 1;
mon = 12;
}
}
d.setDate(d.getDate() - n);
year = d.getFullYear();
mon = d.getMonth() + 1;
day = d.getDate();
var s = year + "-" + (mon < 10 ? ('0' + mon) : mon) + "-" + (day < 10 ? ('0' + day) : day);
return s;
}
指定された時間の前、後何分後の時間を取得しますか? //d
function getNowFormatDate(d) {
var seperator1 = "-";
var seperator2 = ":";
//
var minutes=parseInt("30");
var interTimes=minutes*60*1000;
var interTimes=parseInt(interTimes);
date=new Date(Date.parse(d)+interTimes);//+ -
var month = date.getMonth() + 1;
var strDate = date.getDate();
var hour = date.getHours();
var minutes = date.getMinutes();
var s=date.getSeconds();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
if (hour >= 0 && hour <= 9) {
hour = "0" + hour;
}
if (minutes >= 0 && minutes <= 9) {
minutes = "0" + minutes;
}
if (s >= 0 && s <= 9) {
s = "0" + s;
}
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
+ " " + hour + seperator2 + minutes
+ seperator2 + s;
return currentdate;
}
var end_time =getNowFormatDate('2019-07-10 11:14:46');//2019-07-10 11:44:46
jsフォーマット時間 function dateFormat(time) {
var date=new Date(time);
var year=date.getFullYear();
/* , 0 , 0
* 10 0, 09:11:05
* */
var month= date.getMonth()+1<10 ? "0"+(date.getMonth()+1) : date.getMonth()+1;
var day=date.getDate()<10 ? "0"+date.getDate() : date.getDate();
var hours=date.getHours()<10 ? "0"+date.getHours() : date.getHours();
var minutes=date.getMinutes()<10 ? "0"+date.getMinutes() : date.getMinutes();
var seconds=date.getSeconds()<10 ? "0"+date.getSeconds() : date.getSeconds();
//
return year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
}
console.log(dateFormat(Fri Jul 12 2019 00:00:00 GMT+0800))//"2019-07-12 00:00:00"