当前位置:网站首页>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"
综上:
常规的替换只会替换第一次匹配的字符,正则可替换全部
边栏推荐
- Good bosses, please ask the flink CDC oracle to Doris, found that the CPU is unusual, a run down
- LeetCode:899. 有序队列【思维题】
- sqoop ETL tool
- Ant - the design of the Select component using a custom icon (suffixIcon attribute) suffixes, click on the custom ICONS have no reaction, will not display the drop-down menu
- 大佬们,读取mysql300万单表要很长时间,有什么参数可以优惠,或者有什么办法可以快点
- Simple sorting (summer vacation daily question 14)
- 自制蓝牙手机app控制stm8/stm32/C51板载LED
- ssh服务详解
- 说说数据治理中常见的20个问题
- 案例 | 重庆银行流动数据安全挑战及应对实践
猜你喜欢
从图文展示到以云为核,第五代验证码独有的策略情报能力
【学习笔记之菜Dog学C】动态内存管理
2022G1工业锅炉司炉考试练习题及模拟考试
如果禁用了安全启动,GNOME 就会发出警告
In the season of going overseas, the localization of Internet tips for going overseas
Sfdp 超级表单开发平台 V6.0.5 正式发布
共n级台阶,每次可以上1级或2级台阶,有多少种上法?
Flink原理流程图简单记录
[Study Notes Dish Dog Learning C] Dynamic Memory Management
There are n steps in total, and you can go up to 1 or 2 steps each time. How many ways are there?
随机推荐
Dong mingzhu live cold face away, when employees frequency low-level mistakes, no one can understand their products
LeetCode:899. 有序队列【思维题】
验证码业务逻辑漏洞
[QNX Hypervisor 2.2用户手册]10.3 vdev gic
22/8/3(板子)树状dp板子+中国剩余定理+求组合数3,4+容斥原理
How many ways do you know about communication between multiple threads?
小程序:扫码打开参数解析
P3384 【模板】轻重链剖分/树链剖分
In a more general sense, calculating the displacement distance and assumptions
Architecture of the actual combat camp module three operations
2022年茶艺师(中级)考试试题模拟考试平台操作
架构实战营模块三作业
从图文展示到以云为核,第五代验证码独有的策略情报能力
C# 构造函数业务场景测试项目
Utilities of Ruineng Micrometer Chip RN2026
2022年T电梯修理考题及答案
说说数据治理中常见的20个问题
Presto中broadcast join和partition join执行计划的处理过程
Taurus.MVC WebAPI 入门开发教程1:框架下载环境配置与运行(含系列目录)。
Priority_queue element as a pointer, the overloaded operators