300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Date相关日期格式转换

Date相关日期格式转换

时间:2024-07-22 10:48:37

相关推荐

Date相关日期格式转换

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到31yyyy:四位数表示的年份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(); //获取当前日期,如:/9/16myDate.toLocaleTimeString(); //获取当前时间,如:上午12:12:39myDate.toLocaleString(); //获取日期与时间,如:/9/16 上午12:12:39myDate.toJSON(); // 获取json格式日期数据,如:-01-01T19:19:20.253Z

时间字符串转换毫秒的方法:

js 时间转换毫秒的四种方法(转)_weixin_30730053的博客-CSDN博客

/** 直接比较两个字符串格式日期的大小* '-03-31' 或者 '-03-31 12:20:46'*/new Date('-03-31 12:20:46') > new Date('-03-31 12:20:30') // trueDate.parse('-03-31 12:12:30') > Date.parse('-03-31 12:12:29') // true使用new Date() 在小程序中应该将 ‘-’ 转换为 ‘/’,如下:new Date(startTime.replace(/-/g,'/')) > new Date(endTime.replace(/-/g,'/'))

/* =========待优化** 时间格式字符串转换为时间戳* -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");* 如:-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 dateif (typeof time === 'object') {date = time} else {if (typeof time === 'string') {if (/^[0-9]+$/.test(time)) {// support "1548221490638"time = parseInt(time, 10)} else {// support safari// /questions/4310953/invalid-date-in-safaritime = 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 Sundayif (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-11var 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-11if (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-31var relativeDate = new Date(year, month, 1);// 获得当前月份0-11var 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-11var 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(, 3, 10),new Date(, 3, 10),new Date(, 3, 10),]getMinDate(dates) // -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() - 6var startDate = new Date(currentDate.setDate(first))return startTime(startDate)}/**** 上周的结束*/export function getEndLastWeek() {var currentDate = getCurrentDate()var first = currentDate.getDate() - currentDate.getDay() - 6var last = first + 6var 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-11var currentMonth = currentDate.getMonth()//获得当前年份4位年var currentYear = currentDate.getFullYear()//获得上一个月的第一天var priorMonthFirstDay = getPriorMonthFirstDay(currentYear, currentMonth)return priorMonthFirstDay}/**** 上月的最后一天时间*/export function getEndLastMonth() {//获取当前时间var currentDate = getCurrentDate()//获得当前月份0-11var 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-- //年份减1return 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() - 6const startDate = new Date(currentDate.setDate(first))return setHoursStartOrEnd(startDate)}/** ** 上周的结束*/export function getEndLastWeek() {const currentDate = new Date()const first = currentDate.getDate() - currentDate.getDay() - 6const last = first + 6const endDate = new Date(currentDate.setDate(last))return setHoursStartOrEnd(endDate, 'end')}/*** 本月起至时间*/export function getCurrentMonth() {// 起止日期数组const startStop = []// 获取当前时间const currentDate = new Date()// 获得当前月份0-11let 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-11let 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}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。