第 134 题:求两个日期中间的有效日期
如 2015-2-8 到 2015-3-3,返回【2015-2-8 2015-2-9...】
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如 2015-2-8 到 2015-3-3,返回【2015-2-8 2015-2-9...】
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(40)
toLocaleDateString
const getTime = (time1,time2) =>{
let arr = [];
let t1 = new Date(time1).getTime();
let t2 = new Date(time2).getTime();
我这个性能比较差
利用时间戳,➕ 24 * 60 * 60 * 1000,得到下一天时间戳。
function rangeDay (day1, day2) {
const result = []
const dayTimes = 246060*1000
let startTime = day1.getTime()
const endTime = day2.getTime()
while (startTime <= endTime) {
result.push(new Date(startTime).toLocaleDateString().replace(///g, '-'))
startTime += dayTimes;
}
return result
};
console.log(rangeDay(new Date("2015-02-02"), new Date("2015-02-08")));
//题目开放,变式很多了
const dateTransfer = (start, end) => {
const distance = 1000 * 60 * 60 * 24;
const startTime = new Date(start).getTime();
const endTime = new Date(end).getTime();
const result = [];
}
dateTransfer('2019-08-01', '2019-08-30')
function getDays (startDate, endDate) {
let startDateTime = new Date(startDate).getTime()
let endDateTime = new Date(endDate).getTime()
if (isNaN(startDateTime) || isNaN(endDateTime)) {
return '输入的日期不合法'
}
if (endDateTime < startDateTime) {
return '开始时间不能大于结束时间'
}
let days = []
let day = startDateTime
let count = (endDateTime - startDateTime) / 1000 / 60 / 60 / 24
for (let i = 0; i < count; i++) {
day = day + 24 * 60 * 60 * 1000
days.push(new Date(day).toLocaleDateString().replace(///g, '-')) // 这里用 转译一下
}
days.pop()
return days
}
import moment from 'moment';
写一个用moment的
function getTimeList(startTime, endTime) {
let dayList = []
let count = (new Date(endTime).getTime() - new Date(startTime).getTime()) / 1000 / 86400
for (let i=0; i<=count; i++) {
let day = new Date(startTime).getTime() + i100086400
dayList.push(formatDate(day))
}
return dayList
}
function formatDate(now) {
let day = new Date(now)
var year = day.getFullYear();
var month = day.getMonth() + 1;
var date = day.getDate();
return year + "-" + month + "-" + date;
}
console.log(getTimeList('2015-2-8','2015-3-3'))
const getDate = (start,end) => { const result = [] start = new Date(start).getTime() end = new Date(end).getTime() for(let i = start;i<=end;i+=(24*60*60*1000)){ let date = new Date(i) result.push(date.getFullYear()+'-'+(date.getMonth() + 1)+'-'+(date.getDate())) } return result }
["2018-2-8", "2018-2-9", "2018-2-10", "2018-2-11", "2018-2-12", "2018-2-13", "2018-2-14", "2018-2-15", "2018-2-16", "2018-2-17", "2018-2-18", "2018-2-19", "2018-2-20", "2018-2-21", "2018-2-22", "2018-2-23", "2018-2-24", "2018-2-25", "2018-2-26", "2018-2-27", "2018-2-28", "2018-3-1", "2018-3-2", "2018-3-3"]
["2015-2-8", "2015-2-9", "2015-2-10", "2015-2-11", "2015-2-12", "2015-2-13", "2015-2-14", "2015-2-15", "2015-2-16", "2015-2-17", "2015-2-18", "2015-2-19", "2015-2-20", "2015-2-21", "2015-2-22", "2015-2-23", "2015-2-24", "2015-2-25", "2015-2-26", "2015-2-27", "2015-2-28", "2015-3-1", "2015-3-2", "2015-3-3"]