当前位置:网站首页>new Date converts strings into date formats Compatible with IE, how ie8 converts strings into date formats through new Date, how to replace strings in js, and explain the replace() method in detail
new Date converts strings into date formats Compatible with IE, how ie8 converts strings into date formats through new Date, how to replace strings in js, and explain the replace() method in detail
2022-08-04 03:04:00 【cplvfx】
new Date converts strings into date formats compatible with IE, how ie8 converts strings into date formats through new Date, how to replace strings in js, and the replace() method in detail
//Get the year, month, day, hour, minute and second//Incoming date //Example: 2020-10-27T14:36:23var timeFormatSeconds = function(time, type) {var d = time ? new Date(time) : new Date();if(time){time=time.replace(/-/g,"/"); //For compatibility with IEd = new Date(time);}else{d = new Date();}var year = d.getFullYear();var month = d.getMonth() + 1;var day = d.getDate();var hours = d.getHours();var min = d.getMinutes();var seconds = d.getSeconds();if (month < 10) month = '0' + month;if (day < 10) day = '0' + day;if (hours < 0) hours = '0' + hours;if (min < 10) min = '0' + min;if (seconds < 10) seconds = '0' + seconds;var res = "";switch (type) {case 1:res = (year + '-' + month + '-' + day + ' ' + hours + ':' + min);break;case 2:res = (year + '-' + month + '-' + day);break;default:res = (year + '-' + month + '-' + day + ' ' + hours + ':' + min + ':' + seconds);break;}return res;}
The browser will report an error using the replace() method, don't worry, the method execution process is normal
How ie8 converts a string into a date format through new Date
How ie8 converts stringconvert to date format
javaScript ie8 does not support new Date("2017-07-01");
Only supports new Date("2017/07/01")
So in web development, if you need to be compatible with ie, you can use new Date("2017/07/01") to convert the string into time format
Detailed explanation of replace() method
Original
How to replace strings in js-js Tutorial-PHP Chinese Network
Method for string replacement in js:
1. Use regular expressions, the syntax is [str.replace("string to be replaced", "new string")];
2. Use the conventional replacement method, the syntax is [str.replace(/string to be replaced/g, "new string")].
The method of string replacement in js:
Two methods: regular & regular
str.replace("string to be replaced", "new string")
str.replace(/string to be replaced/g, "new string")
For example:
1.
"yyyy-MM-dd-hh-mm-ss".replace("-","/")
The results are as follows:
"yyyy/MM-dd-hh-mm-ss"
2.
"yyyy-MM-dd-hh-mm-ss".replace(/-/g,"/")
The results are as follows:
"yyyy/MM/dd/hh/mm/ss"
In summary:
Regular replacement will only replace the first matching character, regular replacement can replace all
边栏推荐
- SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropri
- C# 构造函数业务场景测试项目
- STM8S project creation (STVD creation) --- use COSMIC to create a C language project
- 【学习笔记之菜Dog学C】动态内存管理
- 自制蓝牙手机app控制stm8/stm32/C51板载LED
- 阿里云国际版基于快照与镜像功能迁移云服务器数据
- Polygon zkEVM网络节点
- db2中kettle报错 Field [XXX] is required and couldn‘t be found 解决方法
- The keytool command
- ssh服务详解
猜你喜欢
随机推荐
Mockito单元测试
STM8S105K4T6------Serial port sending and receiving
织梦响应式酒店民宿住宿类网站织梦模板(自适应手机端)
MallBook联合人民交通出版社,推动驾培领域新发展,开启驾培智慧交易新生态
Zabbix set up email alert + enterprise WeChat alert
esp8266-01s刷固件步骤
Instance, 038: the sum of the diagonal matrix
SQL注入中 #、 --+、 --%20、 %23是什么意思?
activiti流程执行过程中,数据库表的使用关系
APP电商如何快速分润分账?
董明珠直播时冷脸离场,员工频犯低级错误,自家产品没人能弄明白
单片机C语言->的用法,和意思
STM8S project creation (STVD creation) --- use COSMIC to create a C language project
Deep Learning (3) Classification Theory Part
瑞能微计量芯片RN2026的实用程序
QNX Hypervisor 2.2用户手册]10.1 通用vdev选项
共n级台阶,每次可以上1级或2级台阶,有多少种上法?
Mini program + new retail, play the new way of playing in the industry!
ingress 待完善
web端动效 lottie-web 使用