当前位置:网站首页>工具函数---日期格式化
工具函数---日期格式化
2022-04-21 13:39:00 【monstkl】
一、使用场景
一般日期选择控件自带格式化功能,但是当前时间作为默认值,需要我们自己去格式化
二、我的使用总结
//dateTime是时间对象或者时间戳
//formatStr是通过YYYY、MM、DD、HH、mm、SS任意组合的字符串
//如'YYYY-MM-DD|HH-mm-SS'
dateFormat(dateTime, formatStr) {
let date = new Date(dateTime);
var str = formatStr;
str = str.replace(/yyyy|YYYY/, date.getFullYear());
str = str.replace(
/MM/,
date.getMonth() > 9
? (date.getMonth() + 1).toString()
: "0" + (date.getMonth() + 1)
);
str = str.replace(
/dd|DD/,
date.getDate() > 9 ? date.getDate().toString() : "0" + date.getDate()
);
str = str.replace(
/hh|HH/,
date.getHours() > 9 ? date.getHours().toString() : "0" + date.getHours()
);
str = str.replace(
/mm/,
date.getMinutes() > 9
? date.getMinutes().toString()
: "0" + date.getMinutes()
);
str = str.replace(
/ss|SS/,
date.getSeconds() > 9
? date.getSeconds().toString()
: "0" + date.getSeconds()
);
return str;
},
版权声明
本文为[monstkl]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43390711/article/details/121528053
边栏推荐
- Wechat hidden function series: wechat regular reminder, 2 steps, to relax yourself when you are busy
- 二叉树创建及其线索化
- 一级等保怎么做?要收费吗?等保要求是什么?
- About ` object Thoughts on the impossibility of calling clone() ` subclass
- Why does MySQL driver rely on protobuf
- Huffman coding
- An example of expert system and its skeleton system
- [csnote] DB exception (redundant data, modification exception, deletion exception, insertion exception)
- Wanzi dry goods! Help you master the knowledge of "light and shadow" in design
- 通讯录的动态实现
猜你喜欢

If the field lines are the same, they will be merged into another field SQL statement?

AI 视频云 VS 窄带高清,谁是视频时代的宠儿

A great peripheral driver library! (based on stm32f4)

Reverse Polish notation
![BUUCTF [第三章 web进阶]逻辑漏洞](/img/fb/99a5c60979970a9d6cd0fe0e0fb7a4.png)
BUUCTF [第三章 web进阶]逻辑漏洞

Forced to choose an outsourcing company

Code Huffman

Could not load dynamic library ‘libcusolver. so. 11‘

Getting started with Xcode cloud: pull request (PR) workflow

JDBC 和数据库连接池
随机推荐
Route selection, anti ring and re release of hcip OSPF
被迫选择了到了外包公司
完成数亿元融资后,毫末智行计划超百城落地城市智能驾驶产品
Peking University ACM problems 1009: edge detection
Suggestion: Alt + X marks the marked area. Can I cancel the marking?
北京大学ACM Problems 1009:Edge Detection
AI 视频云 VS 窄带高清,谁是视频时代的宠儿
【數字信號處理】相關系數 ( 相關系數概念解析 | 信號能量常數 | 共軛序列 | 序列在相同時刻的相關性 )
Forced to choose an outsourcing company
metasploit渗透
Read fast RCNN
no server suitable for synchronization found
程序员爆出自己的薪资税前月薪15000
After the completion of hundreds of millions of yuan of financing, smart bank plans to land urban intelligent driving products in more than 100 cities
Flash-1.12.0 yarn installation and deployment
JDBC 和数据库连接池
2021-2022 Guangdong Vocational College Students' professional skills competition
科技巨头竞相入场,谁能成为元宇宙“头号玩家”?
Programmers burst out their salary, with a monthly salary of 15000 before tax
国际物流集运系统源码,海外仓储跨境转运系统源码