当前位置:网站首页>万能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;
};边栏推荐
- 不会吧,Log4j 漏洞还没有完全修复?
- 测开基础知识02
- 计算矩阵的逆源码(使用伴随矩阵,3×3的矩阵)
- Linx common directory & file management commands & VI editor usage introduction
- 预测人们对你的第一印象,“AI颜狗”的诞生
- 新人误删数据,组长巧用MySQL主从复制延迟挽回损失
- debian 问题
- 多线程基础(多线程内存,安全,通信,线程池和阻塞队列)
- 和AI一起玩儿剧本杀:居然比我还入戏
- Advanced multi-threading (lock strategy, spin+CAS, Synchronized, JUC, semaphore)
猜你喜欢

MongoDB-CUD without R

预测人们对你的第一印象,“AI颜狗”的诞生

你被MySQL 中的反斜杠 \\坑过吗?

大厂年薪50w+招聘具有测试平台开发能力的测试工程师

Redis 如何实现防止超卖和库存扣减操作?

idea built-in translation plugin

Required request body is missing 问题解决

Test Development Engineer Growth Diary 017 - The Life Cycle of a Bug

DHCP principle and configuration

使用 Grafana 的 Redis Data Source 插件监控 Redis
随机推荐
Bull: remove common characters
如何理解普吕克坐标(几何理解)
华为发布“十大发明”,包含计算、智能驾驶等新领域
MongoDB-CUD without R
远程连接服务器的MySql
The calculation and source code of the straight line intersecting the space plane
matlab机器学习_01
MongoDB - query
Process and Scheduled Task Management
@Bean 与 @Component 用在同一个类上,会怎样?
向量叉乘的几何意义及其模的计算
让百度地图生成器里的“标注”内容展开--解决方案
Polygon 3D(三维平面多边形)的法向量的计算(MeshLab默认的计算)
Multithreading basics (multithreaded memory, security, communication, thread pools and blocking queues)
debian vsftpd + ssl
LVM and disk quotas
schur completement
Software Testing Terminology - Scenario Testing
计算矩阵的逆源码(使用伴随矩阵,3×3的矩阵)
Network Protocol 03 - Routing and NAT