JS date formatting - digital date to Chinese date (encapsulation function, dayjs time format YYYY-MM-DD)

Previous related articles

Article content article link
JS array object——Sort by dateSort by time in ascending or descending order https://blog.csdn.net/XSL_HR/article/details/128579840?spm=1001.2014.3001.5501
JS array object——English sorted by the first letter https://blog.csdn.net/XSL_HR/article/details/128579936?spm=1001.2014.3001.5501
JS array object——Chinese in alphabetical order (grouping) https://blog.csdn.net/XSL_HR/article/details/128580085?spm=1001.2014.3001.5501

Scene recurrence

In the background management system of a team or enterprise, the issuance and generation of certificates will be involved , and various office documents will also be involvedHandling of dates - Chinese format (December 22, 2022)

The effect is as follows :insert image description here


The following describes how to convert the date in digital format (2022-12-22) to the date in Chinese format (December 22, 2022) .

Encapsulation function (digital date to Chinese date)

str is the string form of the date , which can be 2019-04-03, April 03, 2019, must have each character, write 0 for 0 . Ask for YYYYMMDD in full . (If your date format is another format, you can tryType conversion with dayjs[Later articles will introduce the use of dayjs in detail] )

	number2Chinese(str){
    
    
        let chinese=['〇','一','二','三','四','五','六','七','八','九','十'];
        let numStr=str.replace(/[^0-9]+/g, '');
        let year=chinese[numStr[0]]+chinese[numStr[1]]+chinese[numStr[2]]+chinese[numStr[3]];
        let month=numStr[4]==='0'?chinese[numStr[5]]:chinese[10]+chinese[numStr[5]];
        let day='';
        if (numStr[6] === '0') {
    
    
			day = chinese[numStr[7]];
		} else if (numStr[6] === '1') {
    
    
			if (numStr[7] === '0') {
    
    
				day = chinese[10];
			} else {
    
    
				day = chinese[10] + chinese[numStr[7]];
			}
		} else if (numStr[6] === '2' || numStr[6] === '3') {
    
    
			if (numStr[7] === '0') {
    
    
				day = chinese[numStr[6]] + chinese[10];
			} else {
    
    
				day = chinese[numStr[6]] + chinese[10] + chinese[numStr[7]];
			}
		}
        return year+'年'+month+'月'+day+'日';
    }

Explain why this function should be encapsulated :

  • Because you may use this function more than once, if you don’t adopt the idea of ​​encapsulation, you have to rewrite this function every time you use it, so the amount of code is large and the utilization rate is not high.
  • But if you encapsulate, you canReuse in many scenarios, reduce the amount of code, and improve code utilization

insert image description here

practical application

call this function,Pass in the parameter "2022-12-22", print the result on the console.

console.log("日期大写转换",number2Chinese("2022-12-22"))

insert image description here
Obviously, the effect has been achieved. usTry again "December 22, 2022", to see the effect:

console.log("日期大写转换",number2Chinese("2022年12月22日"))

insert image description here
usTry "20221222" again, to see the effect:
insert image description here

❌ But! ! when weWhen entering "2022222", "2022-2-22" or "February 22, 2022", the result is wrong.
insert image description here

This is because the format of the input does not satisfy the standard YYYYMMDD format . So when using this function, time format conversion is required.

next we comeGet the current time and perform date conversion in Chinese format

const nowDate = ref<Dayjs>() // 获取当前时间
const chineseDate = () => {
    
    
    console.log(number2Chinese(
        dayjs(nowDate.value).format("YYYY-MM-DD"))
    )  // dayjs转标准格式 调用日期转中文格式函数 在控制台输出
}
chineseDate()

Result of console conversion format:
insert image description here
digital format conversion to Chinese format has been successful!
insert image description here


The next article will introduce the use of dayjs and the conversion of time format using dayjs~
Interested friends can subscribe to this column to facilitate follow-up learning~
Friends who find this article useful can like it ➕ bookmark ➕ follow it~

Guess you like

Origin blog.csdn.net/XSL_HR/article/details/128607024