当前位置:网站首页>new Date将字符串转化成日期格式 兼容IE,ie8如何通过new Date将字符串转化成日期格式,js中如何进行字符串替换, replace() 方法详解
new Date将字符串转化成日期格式 兼容IE,ie8如何通过new Date将字符串转化成日期格式,js中如何进行字符串替换, replace() 方法详解
2022-08-04 02:53:00 【cplvfx】
new Date将字符串转化成日期格式 兼容IE,ie8如何通过new Date将字符串转化成日期格式,js中如何进行字符串替换, replace() 方法详解
//获得年月日时分秒
//传入日期//例:2020-10-27T14:36:23
var timeFormatSeconds = function(time, type) {
var d = time ? new Date(time) : new Date();
if(time)
{
time=time.replace(/-/g,"/"); //为了兼容IE
d = 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;
}使用 replace() 方法浏览器会报错,不用担心,方法执行过程是正常的
ie8如何通过new Date将字符串转化成日期格式
ie8如何通过new Date将字符串转化成日期格式
javaScript ie8 不支持 new Date("2017-07-01");
只支持new Date("2017/07/01")
所以在web开发中,如果需要兼容ie的话,可以使用new Date("2017/07/01"),将字符串转化成时间格式
replace() 方法详解
原文
js中进行字符串替换的方法:
1、使用正则表达法,语法为【str.replace("需要替换的字符串","新字符串") 】;
2、使用常规的替换方法,语法为【str.replace(/需要替换的字符串/g,"新字符串")】。
js中进行字符串替换的方法:
两种方法:正则&常规
str.replace("需要替换的字符串","新字符串") str.replace(/需要替换的字符串/g,"新字符串")比如:
1、
"yyyy-MM-dd-hh-mm-ss".replace("-","/")结果如下:
"yyyy/MM-dd-hh-mm-ss"2、
"yyyy-MM-dd-hh-mm-ss".replace(/-/g,"/")结果如下:
"yyyy/MM/dd/hh/mm/ss"综上:
常规的替换只会替换第一次匹配的字符,正则可替换全部
边栏推荐
- 怎样提高网络数据安全性
- Utilities of Ruineng Micrometer Chip RN2026
- 2022年T电梯修理考题及答案
- Good bosses, please ask the flink CDC oracle to Doris, found that the CPU is unusual, a run down
- 深度学习(三)分类 理论部分
- Kubernetes:(九)coredns(浪不动了)
- TOML配置文件格式,YAML最有力的竞争者
- 多线程间的通信方式你知道几种?
- Simple sorting (summer vacation daily question 14)
- STM8S105K4T6------Serial port sending and receiving
猜你喜欢

共n级台阶,每次可以上1级或2级台阶,有多少种上法?

Pine Script | How to display and typeset a plot switch?

How to drop all tables under database in MySQL

Architecture of the actual combat camp module three operations

Kubernetes:(十一)KubeSphere的介绍和安装(华丽的篇章)

C program compilation and predefined detailed explanation

案例 | 重庆银行流动数据安全挑战及应对实践

Taurus.MVC WebAPI 入门开发教程1:框架下载环境配置与运行(含系列目录)。

Engineering drawing review questions (with answers)

云开发校园微社区微信小程序源码/二手交易/兼职交友微信小程序开源源码
随机推荐
yum 仅下载包
Big guys, it takes a long time to read mysql3 million single tables, what parameters can be discounted, or is there any way to hurry up
STM8S105K4T6------串口发送和接收
跨境电商看不到另一面:商家刷单、平台封号、黑灰产牟利
mpf5_定价Bond_yield curve_Spot coupon_duration_有效利率_连续复利_远期_Vasicek短期_CIR模型Derivatives_Tridiagonal_ppf
Returns the maximum number of palindromes in a string
系统太多,多账号互通如何实现?
参加Oracle OCP和MySQL OCP考试的学员怎样在VUE预约考试
【指针内功修炼】深度剖析指针笔试题(三)
Example 039: Inserting elements into an ordered list
esp8266-01s刷固件步骤
Example 037: Sorting
Security First: Tools You Need to Know to Implement DevSecOps Best Practices
编写 BOLL 心得体会
22/8/3(板子)树状dp板子+中国剩余定理+求组合数3,4+容斥原理
C语言--环形缓存区
TOML配置文件格式,YAML最有力的竞争者
ssh服务详解
STM8S项目创建(STVD创建)---使用 COSMIC 创建 C 语言项目
JVM内存和垃圾回收-07.堆