当前位置:网站首页>万能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;
};边栏推荐
- The calculation and source code of the straight line intersecting the space plane
- Test and Development Engineer Growth Diary 009 - Environment Pai Pai Station: Development Environment, Test Environment, Production Environment, UAT Environment, Simulation Environment
- MongoDB - query
- 测试开发工程师成长日记010 - Jenkins中的CI/CD/CT(持续集成构建/持续交付/持续测试)
- 从追赶到超越,国产软件大显身手
- Test the basics 01
- prometheus-federation-tls加密
- AI元学习引入神经科学,医疗效果有望精准提升
- matlab机器学习_01
- Rodrigues: vector representation of rotation matrices
猜你喜欢

Advanced multi-threading (lock strategy, spin+CAS, Synchronized, JUC, semaphore)

Data types of Redis6

STL源码剖析:临时对象的代码测试和理解

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

向量三重积的等式推导证明

多线程进阶(CountDownLatch,死锁,线程安全集合类)

STL源码剖析:迭代器的概念理解,以及代码测试。

Polygon 3D(三维平面多边形)的法向量的计算(MeshLab默认的计算)

计算矩阵的逆源码(使用伴随矩阵,3×3的矩阵)

MySql connecting to the server remotely
随机推荐
多线程进阶(锁策略,自旋+CAS,Synchronized,JUC,信号量)
测试开发工程师成长日记018 - 测试面试必备题记录(持续更新)
Linx常见目录&文件管理命令&VI编辑器使用 介绍
Station B collapsed, what would you do if you were the developer in charge that night?
Selenium01
roslyn folder under bin folder
让百度地图生成器里的“标注”内容展开--解决方案
Network Protocol 03 - Routing and NAT
MySQL主从复制配置搭建,一步到位
matlab机器学习_01
MongoDB-CUD没有R
AI元学习引入神经科学,医疗效果有望精准提升
Headline 2: there are several kinds of common SQL errors in MySQL usage?
Redis6的数据类型
多线程进阶(CountDownLatch,死锁,线程安全集合类)
Test development engineer diary 002 - starting from 0 interface automation
Data types of Redis6
Pioneer in Distributed Systems - Leslie Lambert
DNS域名解析服务
使用 Grafana 的 Redis Data Source 插件监控 Redis