当前位置:网站首页>js 常用时间处理函数
js 常用时间处理函数
2022-07-04 06:41:00 【爱技术的大仙】
import moment from 'moment'
// 前面添加0方法
export function add0(m){
return m<10?'0'+m:m
}
export function parseTime(time) {
if (time) {
const date = new Date(time)
const year = date.getFullYear()
/* 在日期格式中,月份是从0开始的,因此要加0 * 使用三元表达式在小于10的前面加0,以达到格式统一 如 09:11:05 * */
const month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
const hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
const minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
const seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
// 拼接
return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
} else {
return ''
}
}
// 获取某月天数
export function getMonthDays(year, month) {
var new_year = year; //取当前的年份
var nextMonth = month++;
if (month>12) {
nextMonth -=12; //月份减
new_year++; //年份增
}
var nextMonthFirstDay=new Date(new_year,nextMonth,1);//下个月第一天
var oneDay=1000*60*60*24;
var dateString=new Date(nextMonthFirstDay-oneDay);
var dateTime = dateString.getDate();
return dateTime;
};
// 获取上个月第一天
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 getTodayDate(){
var today = [];
var todayDate = new Date();
var y = todayDate.getFullYear();
var m = todayDate.getMonth() + 1;
var d = todayDate.getDate();
var s = y+'-'+add0(m)+'-'+add0(d)+' 00:00:00';//今日开始
var e = y+'-'+add0(m)+'-'+add0(d)+' 23:59:59';//今日结束
today.push(s);
today.push(e);
return(today);
}
// 获取昨天时间
export function getYesterdayDate(){
var dateTime = [];
var today = new Date();
var yesterday = new Date(today.setTime(today.getTime()-24*60*60*1000));
var y = yesterday.getFullYear();
var m = yesterday.getMonth() + 1;
var d = yesterday.getDate();
var s = y+'-'+add0(m)+'-'+add0(d)+' 00:00:00';//开始
var e = y+'-'+add0(m)+'-'+add0(d)+' 23:59:59';//结束
dateTime.push(s);
dateTime.push(e);
return(dateTime);
}
// 获取本周开始时间结束时间
export function getCurrentWeek(){
const startStop = new Array();
//获取当前时间
const currentDate = new Date();
//返回date是一周中的某一天
const week = currentDate.getDay();
//返回date是一个月中的某一天
const month = currentDate.getDate();
//一天的毫秒数
const millisecond = 1000 * 60 * 60 * 24;
//减去的天数
const minusDay = week != 0 ? week - 1 : 6;
//alert(minusDay);
//本周 周一
const monday = new Date(currentDate.getTime() - (minusDay * millisecond));
//本周 周日
const sunday = new Date(monday.getTime() + (6 * millisecond));
const sy = monday.getFullYear();
const sm = monday.getMonth() + 1;
const sd = monday.getDate();
const ey = sunday.getFullYear();
const em = sunday.getMonth() + 1;
const ed = sunday.getDate();
const s = sy+'-'+add0(sm)+'-'+add0(sd)+' 00:00:00';//开始
const e = ey+'-'+add0(em)+'-'+add0(ed)+' 23:59:59';//结束
startStop.push(s);
startStop.push(e);
return startStop;
}
// 获取上周时间
export function getLastWeek(){
//起止日期数组
const startStop = new Array();
//获取当前时间
const currentDate = new Date();
//返回date是一周中的某一天
const week = currentDate.getDay();
//返回date是一个月中的某一天
const month = currentDate.getDate();
//一天的毫秒数
const millisecond = 1000 * 60 * 60 * 24;
//减去的天数
const minusDay = week != 0 ? week - 1 : 6;
//获得当前周的第一天
const currentWeekDayOne = new Date(currentDate.getTime() - (millisecond * minusDay));
//上周最后一天即本周开始的前一天
const priorWeekLastDay = new Date(currentWeekDayOne.getTime() - millisecond);
//上周的第一天
const priorWeekFirstDay = new Date(priorWeekLastDay.getTime() - (millisecond * 6));
const sy = priorWeekFirstDay.getFullYear();
const sm = priorWeekFirstDay.getMonth() + 1;
const sd = priorWeekFirstDay.getDate();
const ey = priorWeekLastDay.getFullYear();
const em = priorWeekLastDay.getMonth() + 1;
const ed = priorWeekLastDay.getDate();
const s = sy+'-'+add0(sm)+'-'+add0(sd)+' 00:00:00';//开始
const e = ey+'-'+add0(em)+'-'+add0(ed)+' 23:59:59';//结束
startStop.push(s);
startStop.push(e);
return startStlet
}
//获取本月时间
export function getCurrentMonth(){
//起止日期数组
let startStop = new Array();
//获取当前时间
let currentDate = new Date();
//获得当前月份0-11
let currentMonth = currentDate.getMonth();
//获得当前年份4位年
let currentYear = currentDate.getFullYear();
//求出本月第一天
let firstDay = new Date(currentYear, currentMonth, 1);
//当为12月的时候年份需要加1
//月份需要更新为0 也就是下一年的第一个月
if (currentMonth == 11) {
currentYear++;
currentMonth = 0; //就为
} else {
//否则只是月份增加,以便求的下一月的第一天
currentMonth++;
}
//一天的毫秒数
let millisecond = 1000 * 60 * 60 * 24;
//下月的let
let nextMonthDayOne = new Date(currentYear, currentMonth, 1);
//求出上月的最后一天
let lastDay = new Date(nextMonthDayOne.getTime() - millisecond);
let sy = firstDay.getFullYear();
let sm = firstDay.getMonth() + 1;
let sd = firstDay.getDate();
let ey = lastDay.getFullYear();
let em = lastDay.getMonth() + 1;
let ed = lastDay.getDate();
let s = sy+'-'+add0(sm)+'-'+add0(sd)+' 00:00:00';//开始
let e = ey+'-'+add0(em)+'-'+add0(ed)+' 23:59:59';//结束
startStop.push(s);
startStop.push(e);
return startStop;
}
// 获取上月时间
export function getLastMonth(){
let startStop = new Array();
//获取当前时间
let currentDate = new Date();
//获得当前月份0-11
let currentMonth = currentDate.getMonth();
//获得当前年份4位年
let currentYear = currentDate.getFullYear();
//获得上一个月的第一天
let priorMonthFirstDay = getPriorMonthFirstDay(currentYear, currentMonth);
//获得上一月的最后一天
let priorMonthLastDay = new Date(priorMonthFirstDay.getFullYear(), priorMonthFirstDay.getMonth(), getMonthDays(priorMonthFirstDay.getFullYear(), priorMonthFirstDay.getMonth()));
let sy = priorMonthFirstDay.getFullYear();
let sm = priorMonthFirstDay.getMonth() + 1;
let sd = priorMonthFirstDay.getDate();
let ey = priorMonthLastDay.getFullYear();
let em = priorMonthLastDay.getMonth() + 1;
let ed = priorMonthLastDay.getDate();
let s = sy+'-'+add0(sm)+'-'+add0(sd)+' 00:00:00';//开始
let e = ey+'-'+add0(em)+'-'+add0(ed)+' 23:59:59';//结束
startStop.push(s);
startStop.push(e);
return startStop;
}
// 不能选择今天之前的时间
// 判断传入时间是否小于今天0时0分0秒0毫秒的时间戳
// 如果不传入参数,则传入null,即用moment()来比较
// setHours用于设置指定的(小时[, 分钟[, 秒[, 毫秒]]])
// dateObj.setHours(hoursValue[, minutesValue[, secondsValue[, msValue]]])
// 如果不指定 minutesValue,secondsValue 和 msValue 参数,则会使用getMinutes(),getSeconds() 和getMilliseconds() 方法的返回值。
// 组件用法{/* 不能选择今天之前的日期 */}
// <DatePicker
// showTime
// allowClear
// disabledDate={isBefore}
// style={
{ width: '100%' }}
// ></DatePicker>
export function isBefore(date) {
const today = new Date().setHours(0, 0, 0, 0)
return moment(date).isBefore(today)
}
/* 格式化日期 */
export function formateDate(time) {
if (!time) return ''
let date = new Date(time)
return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() +
' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds()
}
// 倒计时
// 函数接收两个参数,当前时间和结束时间(使用毫秒)。调用一次后,时间会开始倒计时~
export const countDown = (current, ends) => {
const leftTime = ends - current;
let [h, m, s] = ['00', '00', '00'];
if (leftTime >= 0) {
h =
Math.floor((leftTime / 1000 / 60 / 60) % 24) >= 10
? Math.floor((leftTime / 1000 / 60 / 60) % 24)
: `0${
Math.floor((leftTime / 1000 / 60 / 60) % 24)}`;
m =
Math.floor((leftTime / 1000 / 60) % 60) >= 10
? Math.floor((leftTime / 1000 / 60) % 60)
: `0${
Math.floor((leftTime / 1000 / 60) % 60)}`;
s =
Math.floor((leftTime / 1000) % 60) >= 10
? Math.floor((leftTime / 1000) % 60)
: `0${
Math.floor((leftTime / 1000) % 60)}`;
}
setTimeout(countDown, 1000);
}
// 获取当前年月季度
export const getYearSeason =()=>{
const nowDate = new Date()
const y = nowDate.getFullYear() // 年
const currMonth = nowDate.getMonth() + 1 //月
const currQuarter = Math.floor((currMonth % 3 === 0 ? (currMonth / 3) : (currMonth / 3 + 1))) // 季度
return {
y,currQuarter,currMonth}
}
边栏推荐
- Mysql 45讲学习笔记(十)force index
- 金盾视频播放器拦截的软件关键词和进程信息
- Inputstream/outputstream (input and output of file)
- centos8安装mysql.7 无法开机启动
- 2022年,或許是未來10年經濟最好的一年,2022年你畢業了嗎?畢業後是怎麼計劃的?
- Selection (021) - what is the output of the following code?
- tcp socket 的 recv 如何接收指定长度消息?
- [MySQL] introduction, function, creation, view, deletion and modification of database view (with exercises)
- Manually page the list (parameter list, current page, page size)
- Sleep quality today 78 points
猜你喜欢
Inputstream/outputstream (input and output of file)
17-18. Dependency scope and life cycle plug-ins
MySQL 45 lecture learning notes (VII) line lock
Mysql 45讲学习笔记(七)行锁
The solution of win11 taskbar right click without Task Manager - add win11 taskbar right click function
Distributed cap theory
MySQL learning notes 3 - JDBC
Download kicad on Alibaba cloud image station
Cervical vertebra, beriberi
P26-P34 third_ template
随机推荐
高薪程序员&面试题精讲系列119之Redis如何实现分布式锁?
Summary of leetcode BFS question brushing
Tar source code analysis 8
How to use multithreading to export excel under massive data? Source code attached!
SQL join, left join, right join usage
2022, peut - être la meilleure année économique de la prochaine décennie, avez - vous obtenu votre diplôme en 2022? Comment est - ce prévu après la remise des diplômes?
采用中微BATG135实现IIC数据/指令交互
How does the recv of TCP socket receive messages of specified length?
tars源码分析之9
[backpack DP] backpack problem
Option (024) - do all objects have prototypes?
《ClickHouse原理解析与应用实践》读书笔记(4)
leetcode 310. Minimum Height Trees
leetcode825. 适龄的朋友
抽奖系统测试报告
STM32 单片机ADC 电压计算
Tar source code analysis 6
2022 where to find enterprise e-mail and which is the security of enterprise e-mail system?
What is tweeman's law?
SQL injection SQL lab 11~22