JS date and string conversion - JS encapsulation function, Dayjs conversion time format
Related Articles Call
Article content | article link |
---|---|
JS array object——Sort by date,Sort by time in ascending or descending order | https://blog.csdn.net/XSL_HR/article/details/128579840?spm=1001.2014.3001.5501 |
JS date time formatting——Convert digital date to Chinese date(encapsulation function, dayjs conversion time format) | https://blog.csdn.net/XSL_HR/article/details/128607024?spm=1001.2014.3001.5501 |
Scene recurrence
In the previous article , we introduced how to convert 2022-12-22 to December 22, 2022 , involvingConvert date format to YYYYMMDD formatThe problem. This article takes this as the main line and uses two methods to lead you to convert the time format.
1. JS encapsulation function
The first is the traditional encapsulation function, write a date-to-string function and encapsulate it for use.
1. Date to string
function dateToString (date){
var year = date.getFullYear();
var month =(date.getMonth() + 1).toString();
var day = (date.getDate()).toString();
if (month.length == 1) {
month = "0" + month;
}
if (day.length == 1) {
day = "0" + day;
}
var dateTime = year + "-" + month + "-" + day;
return dateTime;
},
The console call prints the result:
console.log(dateToString("Wed Jan 04 2023 14:12:56 GMT+0800 (中国标准时间) "))
2. String to date
function stringToDate (dateStr,separator){
if (!separator){
separator= "-" ;
}
var dateArr = dateStr.split(separator);
var year = parseInt(dateArr[0]);
var month;
if (dateArr[1].indexOf( "0" ) == 0){
month = parseInt(dateArr[1].substring(1));
} else {
month = parseInt(dateArr[1]);
}
var day = parseInt(dateArr[2]);
var date = new Date(year,month -1,day);
return date;
}
The console call prints the result:
console.log(stringToDate("2022-12-22"))
At this time, the time format has been converted from "2022-12-22" to
"Thu Dec 22 2022 00:00:00 GMT+0800 (China Standard Time)" [ However, generally you will not be allowed to convert the time into Such. Both require conversion from date to stringYYYY-MM-DDformat ]
Two, Dayjs conversion time format
1. Quick installation and use of Dayjs
To use Day.js in a Node.js project, simplyInstall using npm。
npm install dayjs
The node.js installation tutorial will not be described too much here, you can read related tutorials in previous articles
ThenIntroduced in the project codeYou can:
const dayjs = require('dayjs')
//import dayjs from 'dayjs' // ES 2015
dayjs().format()
2. Dayjs formatted date
When format conversion is not performed:
const nowDate = ref<Dayjs>() // 获取当前时间
console.log(dayjs("当前时间",nowDate.value))
After time conversion:
const nowDate = ref<Dayjs>() // 获取当前时间
console.log("当前时间",(dayjs(nowDate.value).format("YYYY-MM-DD")) // dayjs进行时间转换
more placeholders:
enter | example | details |
---|---|---|
YY | 01 | two digit year |
YYYY | 2001 | four digit year |
M | 1-12 | four digit year |
MM | 01-12 | month, starting from 1 |
MMM | Jan-Dec | abbreviated month name |
MMMM | January-December | full month name |
D | 1-31 | day of the month |
DD | 01-31 | day of the month, two digits |
H | 0-23 | Hour |
HH | 0-23 | hours, two digits |
h | 1-12 | hour, 12-hour format |
hh | 01-12 | Hour, 12-hour clock, two digits |
m | 0-59 | minute |
mm | 00-59 | minutes, two digits |
s | 0-59 | Second |
ss | 00-59 | two digits of seconds |
S | 0-9 | milliseconds, one digit |
SS | 00-99 | milliseconds, two digits |
SSS | 000-999 | milliseconds, three digits |
A | AM PM | capitalized am afternoon |
a | a.m. p.m | am afternoon lowercase |