foreword
I haven’t written a blog for a long time. I have come into contact with many and complicated things recently. I have no clue when I write. I want to summarize Date because it is a practical and commonly used library. Mastering the basic methods can speed up data processing.
Table of contents
- basic grammar
- Date basic method
- Date advanced use
- Date format processing
- application
1. Basic grammar
Get the current time: today.value = new Date()
Date() can pass parameters internally
2. Basic method
There are two main references, local time and universal time
local time
world time
Universal time UT is the Greenwich mean solar time, which refers to the standard time where Greenwich is located, and is also a form of the earth's rotation rate.
3. Advanced method
Use the provided basic methods to quickly calculate common times
const computeRange = () => {
const date = new Date()
const today = date.getDay()
time.yesturday = new Date(date.getTime() - 3600 * 1000 * 24)
// thisWeek
let stepMonday = 1 - today
let stepSunDay = 7 - today
if (today === 0) {
stepMonday = 1 - 7
stepSunDay = 7 - 7
}
const monday = new Date(date.getTime() + stepMonday * 24 * 3600 * 1000)
const sunday = new Date(date.getTime() + stepSunDay * 24 * 3600 * 1000)
time.thisWeek = [formatDate(monday), formatDate(sunday)]
// lastWeek
let lastMonday = 1 - today - 7
let lastSunDay = 7 - today - 7
if (today === 0) {
lastMonday = 1 - 7 - 7
lastSunDay = 7 - 7 - 7
}
const lmonday = new Date(date.getTime() + lastMonday * 24 * 3600 * 1000)
const lsunday = new Date(date.getTime() + lastSunDay * 24 * 3600 * 1000)
time.lastWeek = [formatDate(lmonday), formatDate(lsunday)]
// thismonth
const firstDay = new Date(date.getFullYear(), date.getMonth(), 1)
const lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0)
time.thisMonth = [formatDate(firstDay), formatDate(lastDay)]
// lastmonth
const LfirstDay = new Date(date.getFullYear(), date.getMonth() - 1, 1)
const LlastDay = new Date(date.getFullYear(), date.getMonth(), 0)
time.lastMonth = [formatDate(LfirstDay), formatDate(LlastDay)]
// thisyear
const yearFirstDay = new Date(date.getFullYear(), 0, 1)
const yearLastDay = new Date(date.getFullYear() + 1, 0, 0)
time.thisYear = [formatDate(yearFirstDay), formatDate(yearLastDay)]
// lastyear
const lastYearFirstDay = new Date(date.getFullYear() - 1, 0, 1)
const lastYearLastDay = new Date(date.getFullYear(), 0, 0)
time.lastYear = [formatDate(lastYearFirstDay), formatDate(lastYearLastDay)]
}
formatDate() Time formatting method, if not formatted, the style of time should be similar to that of new Date()
import dateformat from 'dateformat'
// 格式化日期
const formatDate = (t) => {
return t ? dateformat(t, 'yyyy-mm-dd') : null
}
export default formatDate
time format information
Different processing of time can make better use of time
Normal time is the basis of everything, but it is often inconvenient to use.
Timestamp is the time value converted from this time, which is convenient for storage and comparison.
Formatting is usually for better display of this time.
time formatting
There are two ways, use the ready-made library and format the time by yourself. The ready-made library is more convenient, but you must abide by the rules of its definition of time, and you can customize the relevant parameters by writing your own method
The dateformat library
can be used by directly downloading it through npm.
For details, see the address
https://www.npmjs.com/package/dateformat/v/5.0.3
This is the format of the relevant format.
create library yourself
You can create a new Date-related method, mainly to specify the formatting parameters of each time
Just quote it directly when you want to use it
5. Application
Process the list of data from the 1st of this month to today
var today = new Date()
const year = today.getFullYear().toString()
const month = (today.getMonth() + 1).toString()
console.log(today.getDate(), 12, today.getFullYear(), today.getMonth())
// 更新日期表
for (let index = 1; index <= today.getDate(); index++) {
const tryDate = year + ('00' + month).slice(-2) + ('00' + index).slice(-2)
console.log(tryDate)
const data = {
flowDate: tryDate, flowUsed: 0 }
this.state.dayFlow.push(data)
}