标签:day length string rand time push reverse set 时间
js获取日期时间格式
export function formatDateTime(timeStamp) {
var date = new Date();
date.setTime(timeStamp);
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
var d = date.getDate();
d = d < 10 ? ('0' + d) : d;
var h = date.getHours();
h = h < 10 ? ('0' + h) : h;
var minute = date.getMinutes();
var second = date.getSeconds();
minute = minute < 10 ? ('0' + minute) : minute;
second = second < 10 ? ('0' + second) : second;
return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
}
js获取日期
export function formatDate(timeStamp) {
var date = new Date();
date.setTime(timeStamp);
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
var d = date.getDate();
d = d < 10 ? ('0' + d) : d;
return y + '-' + m + '-' + d;
};
js获取时间格式
export function formatTime(timeStamp) {
var date = new Date();
date.setTime(timeStamp);
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
var d = date.getDate();
d = d < 10 ? ('0' + d) : d;
var h = date.getHours();
h = h < 10 ? ('0' + h) : h;
var minute = date.getMinutes();
var second = date.getSeconds();
minute = minute < 10 ? ('0' + minute) : minute;
second = second < 10 ? ('0' + second) : second;
return h+':'+minute+':'+second;
};
获取近7天,近15天等的所有日期
getDate(7) // ["2019-04-17", "2019-04-18", "2019-04-19", "2019-04-20", "2019-04-21", "2019-04-22", "2019-04-23"]
getDate(15)
function getDate(num){
let arr = [];
for (let index = 0; index < num; index++) {
arr.push(getDay(-index))
}
return arr.reverse();
}
export function getDay(day){
var today = new Date();
var targetday_milliseconds=today.getTime() + 1000*60*60*24*day;
today.setTime(targetday_milliseconds); //注意,这行是关键代码
var tYear = today.getFullYear();
var tMonth = today.getMonth();
var tDate = today.getDate();
tMonth = doHandleMonth(tMonth + 1);
tDate = doHandleMonth(tDate);
return tYear+"-"+tMonth+"-"+tDate;
}
function doHandleMonth(month){
var m = month;
if(month.toString().length == 1){
m = "0" + month;
}
return m;
}
获取某一天日期到某一天日期的所有日期
getYearAndMonthAndDay('2019-4-20','2019-4-23')
function nextDay(date,end,arr){
arr.push( formatDate(date) )
var date = date + 1000*60*60*24*1;
if(date<end){
nextDay(date,end,arr);
}
return arr;
}
function getYearAndMonthAndDay(start,end){
var result = [];
var start_time = new Date(start).getTime();
var end_time = new Date(end).getTime();
result = nextDay(start_time,end_time,[]);
result.push( formatDate(end));
? ? return result;
}
标签:day length string rand time push reverse set 时间
原文地址:https://www.cnblogs.com/hibiscus-ben/p/10757393.html