当前位置:网站首页>万能js时间日期格式转换
万能js时间日期格式转换
2022-07-30 05:50:00 【cplvfx】
调用
//调用方式一
DateTimeConvert('2022-07-21 09:03:40','yyyy-mm-dd')
//调用方式二
DateTimeConvert('2022-07-21 09:03:40','yyyy-mm-dd hh:mm')
//调用方式三
DateTimeConvert('2022-07-21 09:03:40')
输出
输出类型一:
DateTimeConvert('2022-07-21 09:03:40','yyyy-mm-dd')
结果:"2022-07-21"
输出类型二:
DateTimeConvert('2022-07-21 09:03:40','yyyy-mm-dd hh:mm')
结果:"2022-07-21 9:03"
输出类型三:
DateTimeConvert('2022-07-21 09:03:40')
结果:"2022-07-21 9:03:40"
核心转换方法
function DateTimeConvert(time,type) {
var d = time ? new Date(time) : 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 'yyyy-mm-dd':
res=(year + '-' + month + '-' + day);
break;
case 'yyyy-mm-dd hh:mm':
res=(year + '-' + month + '-' + day + ' ' + hours + ':' + min);
break;
default:
res=(year + '-' + month + '-' + day + ' ' + hours + ':' + min + ':' + seconds);
break;
}
return res;
};
边栏推荐
- (GGG)JWT
- Test Development Engineer Growth Diary 018 - Record of Required Questions for Test Interview (Continuous Update)
- Detailed explanation of numpy multidimensional array ndarray
- Required request body is missing problem solving
- 新人误删数据,组长巧用MySQL主从复制延迟挽回损失
- debian 问题
- 这个终端连接工具,碾压Xshell
- Selenium02
- 05-Theos
- prometheus-tls加密
猜你喜欢
Huawei released "ten inventions", including computing, intelligent driving and other new fields
空间直线到平面上的交点的计算证明及其源码
prometheus-federation-tls加密
(GGG)JWT
空间平面相交的直线的计算及其源码
Test Development Engineer Growth Diary 018 - Record of Required Questions for Test Interview (Continuous Update)
Local Implicit Grid Representations for 3D Scenes详解
Process and Scheduled Task Management
Multithreading basics (multithreaded memory, security, communication, thread pools and blocking queues)
MongoDB-CUD without R
随机推荐
B站崩了,如果是你是那晚负责的开发人员你会怎么做?
这个终端连接工具,碾压Xshell
Bull: remove common characters
idea内置翻译插件
Advanced multi-threading (CountDownLatch, deadlock, thread-safe collection class)
矩阵的行列式的计算及其源码
DHCP原理与配置
DHCP principle and configuration
MongoDB-CUD without R
Rodrigues: vector representation of rotation matrices
Redis 如何实现防止超卖和库存扣减操作?
AI can identify race from X-rays, but no one knows why
Selenium01
proftpd 配置文件说明
MySQL什么时候用表锁,什么时候用行锁?
About memcache kernel, so one of the most popular
测开基础知识01
删除openstack中的僵尸实例
Test development engineer growth diary 016 - those things about the test
空间直线到平面上的交点的计算证明及其源码