当前位置:网站首页>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
边栏推荐
猜你喜欢

6口全千兆二层网管型工业以太网交换机千兆2光4电光纤自愈ERPS环网交换机

2千兆光+6千兆电导轨式网管型工业级以太网交换机支持X-Ring冗余环网一键环网交换机

瑞能微计量芯片RN2026的实用程序

4路双向HDMI综合业务高清视频光端机8路HDMI高清视频光端机
![[Playwright Test Tutorial] 5 minutes to get started](/img/68/36dd8ef4a4073f03d5e5dad91be20d.png)
[Playwright Test Tutorial] 5 minutes to get started

融云「音视频架构实践」技术专场【内含完整PPT】

sqoop ETL tool

Zabbix set up email alert + enterprise WeChat alert

【Playwright测试教程】5分钟上手

MySQL高级-读写分离-分库分表
随机推荐
yum 仅下载包
逻辑漏洞----其他类型
STM8S project creation (STVD creation) --- use COSMIC to create a C language project
Development of Taurus. MVC WebAPI introductory tutorial 1: download environment configuration and operation framework (including series directory).
[Study Notes Dish Dog Learning C] Dynamic Memory Management
LeetCode:899. 有序队列【思维题】
web端动效 lottie-web 使用
千兆2光8电管理型工业以太网交换机WEB管理X-Ring一键环网交换机
Example 041: Methods and variables of a class
new Date将字符串转化成日期格式 兼容IE,ie8如何通过new Date将字符串转化成日期格式,js中如何进行字符串替换, replace() 方法详解
三分建设,七分管理!产品、系统、组织三管齐下节能降耗
Day13 Postman的使用
P3384 【模板】轻重链剖分/树链剖分
一个属于程序员的七夕节!
Architecture of the actual combat camp module three operations
[Original] Start the XPS/OXPS reader that comes with Windows 10
C# 构造函数业务场景测试项目
复制带随机指针的链表
TOML配置文件格式,YAML最有力的竞争者
【医保科普】维护医保基金安全,我们可以这样做