当前位置:网站首页>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}
}
边栏推荐
- C语言中的排序,实现从小到大的数字排序法
- 关于IDEA如何设置快捷键集
- Cloud native - SSH article that must be read on the cloud (commonly used for remote login to ECS)
- 【问题记录】03 连接MySQL数据库提示:1040 Too many connections
- SQL injection SQL lab 11~22
- tars源码分析之5
- What is the sheji principle?
- Sleep quality today 78 points
- 高薪程序员&面试题精讲系列119之Redis如何实现分布式锁?
- 【网络数据传输】基于FPGA的百兆网/兆网千UDP数据包收发系统开发,PC到FPGA
猜你喜欢
Wechat applet scroll view component scrollable view area
Fundamentals of SQL database operation
2022 where to find enterprise e-mail and which is the security of enterprise e-mail system?
selenium IDE插件下载安装使用教程
C实现贪吃蛇小游戏
leetcode825. 适龄的朋友
Variables d'environnement personnalisées uniapp
Google Chrome Portable Google Chrome browser portable version official website download method
R statistical mapping - random forest classification analysis and species abundance difference test combination diagram
《ClickHouse原理解析与应用实践》读书笔记(4)
随机推荐
ABCD four sequential execution methods, extended application
SQL join, left join, right join usage
uniapp 自定义环境变量
centos8安装mysql.7 无法开机启动
What is tweeman's law?
Tar source code analysis 6
【GF(q)+LDPC】基于二值图GF(q)域的规则LDPC编译码设计与matlab仿真
tars源码分析之8
Internet of things protocol ZigBee ZigBee module uses the concept of protocol stack
leetcode 310. Minimum Height Trees
11. Dimitt's law
关于IDEA如何设置快捷键集
STC8H开发(十二): I2C驱动AT24C08,AT24C32系列EEPROM存储
MySQL 45 learning notes (XI) how to index string fields
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】数据库视图的介绍、作用、创建、查看、删除和修改(附练习题)
tars源码分析之2
MySQL learning notes 3 - JDBC
Which water in the environment needs water quality monitoring
颈椎、脚气