当前位置:网站首页>JS common time processing functions
JS common time processing functions
2022-07-04 06:41:00 【Technology loving immortal】
import moment from 'moment'
// Add 0 Method
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()
/* In date format , From the month 0 At the beginning , So add 0 * Use a ternary expression in less than 10 In front of the 0, To achieve uniform format Such as 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()
// Splicing
return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
} else {
return ''
}
}
// Get the number of days in a month
export function getMonthDays(year, month) {
var new_year = year; // Take the current year
var nextMonth = month++;
if (month>12) {
nextMonth -=12; // Monthly decrease
new_year++; // Year increase
}
var nextMonthFirstDay=new Date(new_year,nextMonth,1);// The first day of next month
var oneDay=1000*60*60*24;
var dateString=new Date(nextMonthFirstDay-oneDay);
var dateTime = dateString.getDate();
return dateTime;
};
// Get the first day of last month
export function getPriorMonthFirstDay(year, month) {
// Year is 0 representative , It's the first month of this year , So it can't be reduced
if (month == 0) {
month = 11; // The month is the last month of the previous year
year--; // Years minus 1
return new Date(year, month, 1);
}
// otherwise , Subtract only the month
month--;
return new Date(year, month, 1);
};
// Get the start time and end time of the day
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';// Starting today
var e = y+'-'+add0(m)+'-'+add0(d)+' 23:59:59';// End today
today.push(s);
today.push(e);
return(today);
}
// Get yesterday's time
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';// Start
var e = y+'-'+add0(m)+'-'+add0(d)+' 23:59:59';// end
dateTime.push(s);
dateTime.push(e);
return(dateTime);
}
// Get the start time and end time of the week
export function getCurrentWeek(){
const startStop = new Array();
// Get the current time
const currentDate = new Date();
// return date It's one day of the week
const week = currentDate.getDay();
// return date It's one day of the month
const month = currentDate.getDate();
// Milliseconds of the day
const millisecond = 1000 * 60 * 60 * 24;
// Minus the number of days
const minusDay = week != 0 ? week - 1 : 6;
//alert(minusDay);
// This week, Monday
const monday = new Date(currentDate.getTime() - (minusDay * millisecond));
// This week, Sunday
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';// Start
const e = ey+'-'+add0(em)+'-'+add0(ed)+' 23:59:59';// end
startStop.push(s);
startStop.push(e);
return startStop;
}
// Get the time of last week
export function getLastWeek(){
// Start and end date array
const startStop = new Array();
// Get the current time
const currentDate = new Date();
// return date It's one day of the week
const week = currentDate.getDay();
// return date It's one day of the month
const month = currentDate.getDate();
// Milliseconds of the day
const millisecond = 1000 * 60 * 60 * 24;
// Minus the number of days
const minusDay = week != 0 ? week - 1 : 6;
// Get the first day of the current week
const currentWeekDayOne = new Date(currentDate.getTime() - (millisecond * minusDay));
// The last day of last week, the day before the beginning of this week
const priorWeekLastDay = new Date(currentWeekDayOne.getTime() - millisecond);
// The first day of last week
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';// Start
const e = ey+'-'+add0(em)+'-'+add0(ed)+' 23:59:59';// end
startStop.push(s);
startStop.push(e);
return startStlet
}
// Get the time of this month
export function getCurrentMonth(){
// Start and end date array
let startStop = new Array();
// Get the current time
let currentDate = new Date();
// Get the current month 0-11
let currentMonth = currentDate.getMonth();
// Get the current year 4 Year of the year
let currentYear = currentDate.getFullYear();
// Find the first day of the month
let firstDay = new Date(currentYear, currentMonth, 1);
// When it comes to 12 In the month, the year needs to add 1
// The month needs to be updated to 0 That's the first month of the next year
if (currentMonth == 11) {
currentYear++;
currentMonth = 0; // for
} else {
// Otherwise, it's just an increase in months , For the first day of next month
currentMonth++;
}
// Milliseconds of the day
let millisecond = 1000 * 60 * 60 * 24;
// Next month's let
let nextMonthDayOne = new Date(currentYear, currentMonth, 1);
// Find the last day of last month
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';// Start
let e = ey+'-'+add0(em)+'-'+add0(ed)+' 23:59:59';// end
startStop.push(s);
startStop.push(e);
return startStop;
}
// Get the time of last month
export function getLastMonth(){
let startStop = new Array();
// Get the current time
let currentDate = new Date();
// Get the current month 0-11
let currentMonth = currentDate.getMonth();
// Get the current year 4 Year of the year
let currentYear = currentDate.getFullYear();
// Get the first day of last month
let priorMonthFirstDay = getPriorMonthFirstDay(currentYear, currentMonth);
// Get the last day of last month
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';// Start
let e = ey+'-'+add0(em)+'-'+add0(ed)+' 23:59:59';// end
startStop.push(s);
startStop.push(e);
return startStop;
}
// You cannot choose a time before today
// Determine whether the incoming time is less than today 0 when 0 branch 0 second 0 Millisecond timestamps
// If you don't pass in parameters , Then in null, The box moment() To compare
// setHours Used to set the specified ( Hours [, minute [, second [, millisecond ]]])
// dateObj.setHours(hoursValue[, minutesValue[, secondsValue[, msValue]]])
// If you don't specify minutesValue,secondsValue and msValue Parameters , Will use getMinutes(),getSeconds() and getMilliseconds() Return value of method .
// Component usage {/* You cannot select a date before today */}
// <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)
}
/* Format date */
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()
}
// count down
// Function takes two parameters , Current time and end time ( Use milliseconds ). After one call , Time will begin to count down ~
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);
}
// Get the current month, year and quarter
export const getYearSeason =()=>{
const nowDate = new Date()
const y = nowDate.getFullYear() // year
const currMonth = nowDate.getMonth() + 1 // month
const currQuarter = Math.floor((currMonth % 3 === 0 ? (currMonth / 3) : (currMonth / 3 + 1))) // quarter
return {
y,currQuarter,currMonth}
}
边栏推荐
- tars源码分析之5
- 1、 Relevant theories and tools of network security penetration testing
- Tar source code analysis 6
- Summary of leetcode BFS question brushing
- Analysis of tars source code 5
- what the fuck! If you can't grab it, write it yourself. Use code to realize a Bing Dwen Dwen. It's so beautiful ~!
- How does the recv of TCP socket receive messages of specified length?
- STC8H开发(十二): I2C驱动AT24C08,AT24C32系列EEPROM存储
- 图的底部问题
- 24 magicaccessorimpl can access the debugging of all methods
猜你喜欢
what the fuck! If you can't grab it, write it yourself. Use code to realize a Bing Dwen Dwen. It's so beautiful ~!
云原生——上云必读之SSH篇(常用于远程登录云服务器)
ORICO ORICO outdoor power experience, lightweight and portable, the most convenient office charging station
校园网络问题
[March 3, 2019] MAC starts redis
[number theory] fast power (Euler power)
【问题记录】03 连接MySQL数据库提示:1040 Too many connections
leetcode 310. Minimum Height Trees
InputStream/OutputStream(文件的输入输出)
GoogleChromePortable 谷歌chrome浏览器便携版官网下载方式
随机推荐
what the fuck! If you can't grab it, write it yourself. Use code to realize a Bing Dwen Dwen. It's so beautiful ~!
leetcode825. 适龄的朋友
uniapp 自定義環境變量
The cloud native programming challenge ended, and Alibaba cloud launched the first white paper on application liveliness technology in the field of cloud native
云原生——上云必读之SSH篇(常用于远程登录云服务器)
Download kicad on Alibaba cloud image station
Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
Matlab remainder
Mysql 45讲学习笔记(十三)表数据删掉一半,表文件大小不变
8. Factory method
关于IDEA如何设置快捷键集
The solution of win11 taskbar right click without Task Manager - add win11 taskbar right click function
What is tweeman's law?
leetcode 310. Minimum Height Trees
双色球案例
MySQL 45 learning notes (XI) how to index string fields
tars源码分析之10
24 magicaccessorimpl can access the debugging of all methods
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?
MySQL learning notes 3 - JDBC