var myDate = new Date();
// new Date() 参数说明,使用举例
// new Date(yyyy,mth,dd,hh,mm,ss),这种方式,必须传递整型
month:用英文 表示月份名称,从January到December ,缩写也行(Jan....Dec)
mth:用整数表示月份,从0(1月)到11(12月)
dd:表示一个 月中的第几天,从1到31
yyyy:四位数表示的年份
hh:小时数,从0(午夜)到23(晚11点)
mm: 分钟数,从0到59的整数
ss:秒数,从0到59的整数
ms:毫秒数,为大于等于0的整数
myDate.getYear(); //获取当前年份(2位)
myDate.getFullYear(); //获取完整的年份(4位,1970-????)
myDate.getMonth(); //获取当前月份(0-11,0代表1月) 所以获取当前月份是myDate.getMonth()+1
myDate.getDate(); //获取当前日(1-31)
myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数) /获取当前毫秒时间戳
myDate.getHours(); //获取当前小时数(0-23)
myDate.getMinutes(); //获取当前分钟数(0-59)
myDate.getSeconds(); //获取当前秒数(0-59)
myDate.getMilliseconds(); //获取当前毫秒数(0-999)
myDate.toLocaleDateString(); //获取当前日期,如:2020/9/16
myDate.toLocaleTimeString(); //获取当前时间,如:上午12:12:39
myDate.toLocaleString(); //获取日期与时间,如:2020/9/16 上午12:12:39
myDate.toJSON(); // 获取json格式日期数据,如:2020-01-01T19:19:20.253Z
时间字符串转换毫秒的方法:
js 时间转换毫秒的四种方法(转)_weixin_30730053的博客-CSDN博客
/*
* 直接比较两个字符串格式日期的大小
* '2022-03-31' 或者 '2022-03-31 12:20:46'
*/
new Date('2022-03-31 12:20:46') > new Date('2022-03-31 12:20:30') // true
Date.parse('2022-03-31 12:12:30') > Date.parse('2022-03-31 12:12:29') // true
使用new Date() 在小程序中应该将 ‘-’ 转换为 ‘/’,如下:
new Date(startTime.replace(/-/g,'/')) > new Date(endTime.replace(/-/g,'/'))
/* =========待优化
*
* 时间格式字符串转换为时间戳
* 2016-01-01 17:22:37格式 转换为时间戳
*/
function getTimeNumber(date) {
var datetime=new Date(date.replace(/-/g, '/')); //开始时间
return datetime.getTime();
}
// 按照给定格式,将给定日期转换为给定的格式
// 第一种
/** *
* 具体调用: time = formatDate(new Date(), "yyyy-MM-dd hh:mm:ss");
* 如:2020-09-15 23:39:44
*/
export function formatDate(date, fmt) {
date = new Date(date);
var o = {
"M+": date.getMonth() + 1, //月份
"d+": date.getDate(), //日
"h+": date.getHours(), //小时
"m+": date.getMinutes(), //分
"s+": date.getSeconds(), //秒
"q+": Math.floor((date.getMonth() + 3) / 3), //季度
S: date.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(
RegExp.$1,
(date.getFullYear() + "").substr(4 - RegExp.$1.length)
);
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(
RegExp.$1,
RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
);
return fmt;
}
// 第二种
/**
* @param {(Object|string|number)} time
* @param {string} cFormat
* @returns {string | null}
*/
export function parseTime(time, cFormat) {
if (arguments.length === 0 || !time) {
return null
}
const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
let date
if (typeof time === 'object') {
date = time
} else {
if (typeof time === 'string') {
if (/^[0-9]+$/.test(time)) {
// support "1548221490638"
time = parseInt(time, 10)
} else {
// support safari
// https://stackoverflow.com/questions/4310953/invalid-date-in-safari
time = time.replace(new RegExp(/-/gm), '/')
}
}
if (typeof time === 'number' && time.toString().length === 10) {
time = time * 1000
}
date = new Date(time)
}
const formatObj = {
y: date.getFullYear(),
m: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
i: date.getMinutes(),
s: date.getSeconds(),
a: date.getDay()
}
const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
const value = formatObj[key]
// Note: getDay() returns 0 on Sunday
if (key === 'a') {
return ['日', '一', '二', '三', '四', '五', '六'][value]
}
return value.toString().padStart(2, '0')
})
return time_str
}
/** *
* 获取返回DayCount天前的日期
*/
function initDatetoStr(DayCount) {
var dd = new Date();
dd.setDate(dd.getDate() - DayCount);
var y = dd.getFullYear();
var m = dd.getMonth() + 1;//获取当前月份的日期
var d = dd.getDate();
if (m < 10) { m = "0" + m; }
if (d < 10) { d = "0" + d; }
return y + "-" + m + "-" + d;
}
/** *
* 获得本周起止时间
*/
var getCurrentWeek = function() {
// 起止日期数组
var startStop = new Array();
// 获取当前时间
var currentDate = new Date();
// 返回date是一周中的某一天
var week = currentDate.getDay();
// 返回date是一个月中的某一天
var month = currentDate.getDate();
// 一天的毫秒数
var millisecond = 1000 * 60 * 60 * 24;
// 减去的天数
var minusDay = week != 0 ? week - 1 : 6;
// alert(minusDay);
// 本周 周一
var monday = new Date(currentDate.getTime() - minusDay * millisecond);
// 本周 周日
var sunday = new Date(monday.getTime() + 6 * millisecond);
// 添加本周时间
startStop.push(monday); // 本周起始时间
// 添加本周最后一天时间
startStop.push(sunday); // 本周终止时间
// 返回
return startStop;
};
/** *
* 获得本月的起止时间
*/
var getCurrentMonth = function() {
// 起止日期数组
var startStop = new Array();
// 获取当前时间
var currentDate = new Date();
// 获得当前月份0-11
var currentMonth = currentDate.getMonth();
// 获得当前年份4位年
var currentYear = currentDate.getFullYear();
// 求出本月第一天
var firstDay = new Date(currentYear, currentMonth, 1);
// 当为12月的时候年份需要加1
// 月份需要更新为0 也就是下一年的第一个月
if (currentMonth == 11) {
currentYear++;
currentMonth = 0; // 就为
} else {
// 否则只是月份增加,以便求的下一月的第一天
currentMonth++;
}
// 一天的毫秒数
var millisecond = 1000 * 60 * 60 * 24;
// 下月的第一天
var nextMonthDayOne = new Date(currentYear, currentMonth, 1);
// 求出上月的最后一天
var lastDay = new Date(nextMonthDayOne.getTime() - millisecond);
// 添加至数组中返回
startStop.push(firstDay);
startStop.push(lastDay);
// 返回
return startStop;
};
/**
* 得到本季度开始的月份
* @param month 需要计算的月份
***/
var getQuarterSeasonStartMonth = function(month) {
var quarterMonthStart = 0;
var spring = 0; // 春
var summer = 3; // 夏
var fall = 6; // 秋
var winter = 9; // 冬
// 月份从0-11
if (month < 3) {
return spring;
}
if (month < 6) {
return summer;
}
if (month < 9) {
return fall;
}
return winter;
};
/**
* 获得该月的天数
* @param year年份
* @param month月份
* */
var getMonthDays = function(year, month) {
// 本月第一天 1-31
var relativeDate = new Date(year, month, 1);
// 获得当前月份0-11
var relativeMonth = relativeDate.getMonth();
// 获得当前年份4位年
var relativeYear = relativeDate.getFullYear();
// 当为12月的时候年份需要加1
// 月份需要更新为0 也就是下一年的第一个月
if (relativeMonth == 11) {
relativeYear++;
relativeMonth = 0;
} else {
// 否则只是月份增加,以便求的下一月的第一天
relativeMonth++;
}
// 一天的毫秒数
var millisecond = 1000 * 60 * 60 * 24;
// 下月的第一天
var nextMonthDayOne = new Date(relativeYear, relativeMonth, 1);
// 返回得到上月的最后一天,也就是本月总天数
return new Date(nextMonthDayOne.getTime() - millisecond).getDate();
};
/**
* 获得本季度的起止日期
*/
var getCurrentSeason = function() {
// 起止日期数组
var startStop = new Array();
// 获取当前时间
var currentDate = new Date();
// 获得当前月份0-11
var currentMonth = currentDate.getMonth();
// 获得当前年份4位年
var currentYear = currentDate.getFullYear();
// 获得本季度开始月份
var quarterSeasonStartMonth = this.getQuarterSeasonStartMonth(currentMonth); // 外部自定义方法
// 获得本季度结束月份
var quarterSeasonEndMonth = quarterSeasonStartMonth + 2;
// 获得本季度开始的日期
var quarterSeasonStartDate = new Date(
currentYear,
quarterSeasonStartMonth,
1
);
// 获得本季度结束的日期
var quarterSeasonEndDate = new Date(
currentYear,
quarterSeasonEndMonth,
this.getMonthDays(currentYear, quarterSeasonEndMonth) // 外部自定义方法
);
// 加入数组返回
startStop.push(quarterSeasonStartDate);
startStop.push(quarterSeasonEndDate);
// 返回
return startStop;
};
/** *
* 得到本年的起止日期
*
*/
var getCurrentYear = function() {
// 起止日期数组
var startStop = new Array();
// 获取当前时间
var currentDate = new Date();
// 获得当前年份4位年
var currentYear = currentDate.getFullYear();
// 本年第一天
var currentYearFirstDate = new Date(currentYear, 0, 1);
// 本年最后一天
var currentYearLastDate = new Date(currentYear, 11, 31);
// 添加至数组
startStop.push(currentYearFirstDate);
startStop.push(currentYearLastDate);
// 返回
return startStop;
};
/**
* 返回日期数列里最小的日期
* **/
const getMinDate = dates => {
if (!dates) {
throw new Error('Argument(s) is illegal !')
}
if (!dates.length) {
return dates
}
return new Date(Math.min.apply(null, dates)).toISOString()
}
// 举例:
const dates = [
new Date(2018, 3, 10),
new Date(2019, 3, 10),
new Date(2020, 3, 10),
]
getMinDate(dates) // 2018-04-09T16:00:00.000Z
/**
* 日期范围工具类
*/
export function startTime(time) {
const nowTimeDate = new Date(time)
return nowTimeDate.setHours(0, 0, 0, 0)
}
export function endTime(time) {
const nowTimeDate = new Date(time)
return nowTimeDate.setHours(23, 59, 59, 999)
}
/***
* 当前时间
*/
export function getCurrentDate() {
return new Date()
}
/***
* 今天的开始时间
*/
export function getBeginToday() {
return new Date(new Date(new Date().toLocaleDateString()).getTime())
}
/***
* 昨天开始时间
*/
export function getBeginYesterday() {
return startTime(getBeginToday() - 24 * 60 * 60 * 1000)
}
/***
* 昨天结束时间时间
*/
export function getEndYesterday() {
return endTime(getBeginToday() - 24 * 60 * 60 * 1000)
}
/***
* 本周的第一天时间
*/
export function getBeginWeek() {
var currentDate = getCurrentDate()
var week = currentDate.getDay()
//一天的毫秒数
var millisecond = 1000 * 60 * 60 * 24
//减去的天数
var minusDay = week != 0 ? week - 1 : 6
//本周 周一
var monday = new Date(currentDate.getTime() - minusDay * millisecond)
return startTime(monday)
}
/***
* 本周的最后一天时间
*/
export function getEndWeek() {
var currentDate = getCurrentDate()
var week = currentDate.getDay()
//一天的毫秒数
var millisecond = 1000 * 60 * 60 * 24
//减去的天数
var minusDay = week != 0 ? week - 1 : 6
//本周 周日
var monday = new Date(currentDate.getTime() - minusDay * millisecond)
var sunday = new Date(monday.getTime() + 6 * millisecond)
//返回
return endTime(sunday)
}
/***
* 上周的开始
*/
export function getBeginLastWeek() {
var currentDate = getCurrentDate()
var first = currentDate.getDate() - currentDate.getDay() - 6
var startDate = new Date(currentDate.setDate(first))
return startTime(startDate)
}
/***
* 上周的结束
*/
export function getEndLastWeek() {
var currentDate = getCurrentDate()
var first = currentDate.getDate() - currentDate.getDay() - 6
var last = first + 6
var endDate = new Date(currentDate.setDate(last))
return endTime(endDate)
}
/***
* 本月的第一天时间
*/
export function getBeginMonth() {
var currentDate = getCurrentDate()
var currentMonth = currentDate.getMonth()
//获得当前年份4位年
var currentYear = currentDate.getFullYear()
//求出本月第一天
var firstDay = new Date(currentYear, currentMonth, 1)
return firstDay
}
/***
* 本月的最后一天时间
*/
export function getEndMonth() {
//获取当前时间
var currentDate = getCurrentDate()
var fullYear = currentDate.getFullYear()
var month = currentDate.getMonth() + 1 // getMonth 方法返回 0-11,代表1-12月
var endOfMonth = new Date(fullYear, month, 0)
return endTime(endOfMonth)
}
/***
* 上月的第一天时间
*/
export function getBeginLastMonth() {
//获取当前时间
var currentDate = getCurrentDate()
//获得当前月份0-11
var currentMonth = currentDate.getMonth()
//获得当前年份4位年
var currentYear = currentDate.getFullYear()
//获得上一个月的第一天
var priorMonthFirstDay = getPriorMonthFirstDay(currentYear, currentMonth)
return priorMonthFirstDay
}
/***
* 上月的最后一天时间
*/
export function getEndLastMonth() {
//获取当前时间
var currentDate = getCurrentDate()
//获得当前月份0-11
var currentMonth = currentDate.getMonth()
//获得当前年份4位年
var currentYear = currentDate.getFullYear()
//当为12月的时候年份需要加1
//月份需要更新为0 也就是下一年的第一个月
if (currentMonth == 11) {
currentYear++
currentMonth = 0 //就为
} else {
//否则只是月份增加,以便求的下一月的第一天
currentMonth++
}
//一天的毫秒数
var millisecond = 1000 * 60 * 60 * 24
//求出上月的最后一天
var lastDay = new Date(getBeginMonth().getTime() - millisecond)
return endTime(lastDay)
}
/**
* 返回上一个月的第一天Date类型
* @param year 年
* @param month 月
**/
export function getPriorMonthFirstDay(year, month) {
//年份为0代表,是本年的第一月,所以不能减
if (month == 0) {
month = 11 //月份为上年的最后月份
year-- //年份减1
return new Date(year, month, 1)
}
//否则,只减去月份
month--
return new Date(year, month, 1)
}
/**
* 设置小时为开始时间或结束时间
*/
export function setHoursStartOrEnd(time, type = 'start') {
time = time || new Date()
const nowTimeDate = new Date(time)
if (type === 'start') {
return nowTimeDate.setHours(0, 0, 0, 0)
} else {
return nowTimeDate.setHours(23, 59, 59, 999)
}
}
/** *
* 今天的开始时间
*/
export function getBeginToday() {
return new Date(new Date(new Date().toLocaleDateString()).getTime())
}
/**
* 今天的结束时间
*/
export function getEndToday() {
return new Date(setHoursStartOrEnd(new Date(), 'end'))
}
/** *
* 昨天开始时间
*/
export function getBeginYesterday() {
const todayBeginMsec = new Date(getBeginToday()).getTime()
return setHoursStartOrEnd(todayBeginMsec - oneDayMsec)
}
/** *
* 昨天结束时间时间
*/
export function getEndYesterday() {
const todayEndMsec = new Date(getEndToday()).getTime()
return setHoursStartOrEnd(todayEndMsec - oneDayMsec, 'end')
}
/** *
* 本周的第一天时间
*/
export function getBeginWeek() {
const currentDate = new Date()
const week = currentDate.getDay()
// 一天的毫秒数
const millisecond = 1000 * 60 * 60 * 24
// 减去的天数
const minusDay = week != 0 ? week - 1 : 6
// 本周 周一
const monday = new Date(currentDate.getTime() - minusDay * millisecond)
return setHoursStartOrEnd(monday)
}
/** *
* 本周的最后一天时间
*/
export function getEndWeek() {
const currentDate = new Date()
const week = currentDate.getDay()
// 一天的毫秒数
const millisecond = 1000 * 60 * 60 * 24
// 减去的天数
const minusDay = week != 0 ? week - 1 : 6
// 本周 周日
const monday = new Date(currentDate.getTime() - minusDay * millisecond)
const sunday = new Date(monday.getTime() + 6 * millisecond)
// 返回
return setHoursStartOrEnd(sunday, 'end')
}
/** *
* 上周的开始
*/
export function getBeginLastWeek() {
const currentDate = new Date()
const first = currentDate.getDate() - currentDate.getDay() - 6
const startDate = new Date(currentDate.setDate(first))
return setHoursStartOrEnd(startDate)
}
/** *
* 上周的结束
*/
export function getEndLastWeek() {
const currentDate = new Date()
const first = currentDate.getDate() - currentDate.getDay() - 6
const last = first + 6
const endDate = new Date(currentDate.setDate(last))
return setHoursStartOrEnd(endDate, 'end')
}
/**
* 本月起至时间
*/
export function getCurrentMonth() {
// 起止日期数组
const startStop = []
// 获取当前时间
const currentDate = new Date()
// 获得当前月份0-11
let currentMonth = currentDate.getMonth()
// 获得当前年份4位年
let currentYear = currentDate.getFullYear()
// 求出本月第一天
const firstDay = new Date(currentYear, currentMonth, 1)
// 当为12月的时候年份需要加1
// 月份需要更新为0 也就是下一年的第一个月
if (currentMonth == 11) {
currentYear++
currentMonth = 0 // 就为
} else {
// 否则只是月份增加,以便求的下一月的第一天
currentMonth++
}
// 一天的毫秒数
const millisecond = 1000 * 60 * 60 * 24
// 下月的第一天
const nextMonthDayOne = new Date(currentYear, currentMonth, 1)
// 求出上月的最后一天
let lastDay = new Date(nextMonthDayOne.getTime() - millisecond)
lastDay = setHoursStartOrEnd(lastDay, 'end')
// 添加至数组中返回
startStop.push(parseTime(firstDay))
startStop.push(parseTime(lastDay))
// 返回
return startStop
}
export function getLastMonth() {
// 起止日期数组
const startStop = []
// 获取当前时间
const currentDate = new Date()
// 获得当前月份0-11
let currentMonth = currentDate.getMonth()
// 获得当前年份4位年
let currentYear = currentDate.getFullYear()
// 求出当月第一天
const curMonthFirstDay = new Date(currentYear, currentMonth, 1)
// 一天的毫秒数
const millisecond = 1000 * 60 * 60 * 24
// 求出上月的最后一天
let lastDay = new Date(curMonthFirstDay.getTime() - millisecond)
lastDay = setHoursStartOrEnd(lastDay, 'end')
// 月份为0则更改年份为去年
if (currentMonth == 0) {
currentYear--
currentMonth = 11
} else {
currentMonth--
}
// 上个月的第一天
const firstDay = new Date(currentYear, currentMonth, 1)
// 添加至数组中返回
startStop.push(parseTime(firstDay))
startStop.push(parseTime(lastDay))
// 返回
return startStop
}