当前位置:网站首页>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}
}
边栏推荐
- 7. Agency mode
- GoogleChromePortable 谷歌chrome浏览器便携版官网下载方式
- Tsinghua University product: penalty gradient norm improves generalization of deep learning model
- Mysql 45讲学习笔记(十二)MySQL会“抖”一下
- 雲原生——上雲必讀之SSH篇(常用於遠程登錄雲服務器)
- STM32 单片机ADC 电压计算
- Selection (023) - what are the three stages of event propagation?
- Mysql 45讲学习笔记(十)force index
- [problem record] 03 connect to MySQL database prompt: 1040 too many connections
- tars源码分析之2
猜你喜欢

24 magicaccessorimpl can access the debugging of all methods

uniapp 自定义环境变量

MySQL installation and configuration

Learning multi-level structural information for small organ segmentation

2022 Xinjiang's latest eight members (Safety Officer) simulated examination questions and answers

Variables d'environnement personnalisées uniapp

Reading notes of Clickhouse principle analysis and Application Practice (4)

C實現貪吃蛇小遊戲

雲原生——上雲必讀之SSH篇(常用於遠程登錄雲服務器)

Deep understanding of redis -- a new type of bitmap / hyperloglgo / Geo
随机推荐
tars源码分析之6
Cloud native - SSH article that must be read on the cloud (commonly used for remote login to ECS)
图的底部问题
tars源码分析之8
测试用例的设计
Appium基础 — APPium安装(二)
Fundamentals of SQL database operation
C réaliser des jeux de serpents gourmands
颈椎、脚气
Tsinghua University product: penalty gradient norm improves generalization of deep learning model
The solution of win11 taskbar right click without Task Manager - add win11 taskbar right click function
tars源码分析之4
Background and current situation of domestic CDN acceleration
由于dms升级为了新版,我之前的sql在老版本的dms中,这种情况下,如何找回我之前的sql呢?
Tar source code analysis 4
Tar source code analysis Part 7
2022年,或许是未来10年经济最好的一年,2022年你毕业了吗?毕业后是怎么计划的?
运算符<< >>傻瓜式测试用例
Sleep quality today 78 points
MySQL learning notes 3 - JDBC