This is the 26th day of my participation in the August More Text Challenge

Persistence in learning, notes is the soul, review the old to learn new, from time to time, review the knowledge point, deepen memory, get twice the result with half the effort!

Earlier we learned about the String object ‘in JavaScript

Today we’re going to learn about Date objects in JavaScript

JavaScript date handling library recommendation:

1. Moment.js

In daily project development, we used the date wrapper library: moment.js to deal with dates

Moment.js momentjs.com

2. day.js

It is now recommended to use a lightweight solution: a more lightweight library: day.js-dayjs.gize.io

Day.js – The open source JavaScript date and time processing library is a lightweight JavaScript date and time processing library that is only 2KB in size. The API design of Moment

Day.js runs in browsers and Node.js.

dayjs().format() // 2021-08-27T23:16:05+08:00
dayjs().format('MMMM DD YYYY, h:mm:ss a') // August 27 2021, 11:16:05 pm
dayjs().format('dddd') // Friday
dayjs().format('MMM DDo YY') // Aug 27o 21
dayjs().format('YYYY [escaped] YYYY') // 2021 escaped 2021
Copy the code

Installation and use:

yarn add dayjs             # Yarn
npm install dayjs --save   # npm
Copy the code
  1. Used in node.js projects:
var dayjs = require('dayjs')
//import dayjs from 'dayjs' // ES 2015
dayjs().format()
By default, the string "2021-08-27T22:59:19+08:00" is returned in ISO8601 format.
Copy the code
  1. Use in the browser
<script src="https://unpkg.com/[email protected]/dayjs.min.js"></script>
<script>
  console.log(dayjs().format())
</script>
Copy the code

JavaScript Date object

The Date function

In project development, it is often necessary to use logic to deal with time, such as various weird countdowns, which have many potential bugs

JavaScript Date objects have a lot of methods, but most of them are getters and setters, and there are only a few that are often used

# Getter method A setter method
1 getDate setDate
2 getDay
3 getFullYear setFullYear
4 getYear setYear
5 getMonth setMonth
6 getHours setHours
7 getMilliseconds setMilliseconds
8 getSeconds setSeconds
9 getUTCDate setUTCDate
10 getUTCDay setUTCDay
.
Conversion method toDateString
toLocalString
toLocalDateString
toTimeString
toLocalTimeString
. . .

The return value of the getMonth method starts at 0, meaning that the return value of getMonth ranges from 0 to 11

The getDate method returns a value from 1 and ranges from 1 to 31. This inconsistency is also a common source of errors.

In addition, it is recommended to use getFullYear and setFullYear to reduce the occurrence of avoidance errors

In our programming mind, the Date function should be a data object with convenient attributes:

  • year
  • month
  • day
  • hour
  • minute
  • second
  • week
  • weekday

Instead of the various traditional methods in the table above, we need some simple methods to handle transitions between the various representations of dates.

The timestamp used by JavaScript is from 1970-01-01,

Date.now(), for example, returns the current timestamp, but can be executed in any JavaScript code, providing an “interface” to maliciously tampered code…

So for daily development, it’s easier to use the packaged date-time library…

Read more

Read more articles please check:

【JS】 A JavaScript- string object, a Boolean type based on object programming

[JS] Exception (error) processingtry-catch

Various magical bugs in [JS]

【 Data structure 】 data structure – objects and arrays (2) Array Array, 【 data structure 】 data structure – objects and arrays (1) Object, 【 data structure 】 in-depth understanding of JSON

Node.js file system server – emulated interface

[Node.js] file system module, [Node.js] HTTP module

[Node.js] efficiency tools – NVM & NRM, etc

Node.js Package management tool NPM and YARN

【Node.js】 Build automated development environment – basic introduction

[Node.js] Installation & Documentation, [Tool Preparation], [Start], [Detailed Steps (4)], [Module processing tools (5)], [Understanding of modular programming]

【Github】 Multi-user collaboration (2), 【Github】 Basic use (1)

【Git】 Code version control – Basic Operation (1)

Keep up the pace, step by step! Updates below:

Next will continue to learn JavaScript related methods in detail, chong Duck!! xdm

Learn the efficiency tools to improve development efficiency and empower our development!

Keep up the pace and keep moving forward

Come on!! go~~