js 字符串格式化为时间格式
2024-10-19 01:31:27
首先介绍一下我遇到的坑,找了几个关于字符串转时间的,他们都可以就我用的时候不行。
我的原因,我的字符串是MYSQL拿出来的不是标准的时间格式,是不会转成功的。
解决思路:先将字符串转为标准时间格式的字符串,在转为时间
原文链接:https://www.jb51.net/article/66385.htm
//字符串转日期格式,strDate要转为日期格式的字符串
function getDate(strDate) {
var st = strDate;
var a = st.split(" "); //这个根据你的字符串决定,如果中间为T则改T
var b = a[0].split("-");
var c = a[1].split(":");
var date = new Date(b[0], b[1], b[2], c[0], c[1], c[2]);
return date;
}
然后就是网上各种转换方式了:
原文链接:https://blog.csdn.net/yangxiaovip/article/details/40429155
function formatDate(date, format) {
if (!date) return;
if (!format) format = "yyyy-MM-dd";
switch(typeof date) {
case "string":
date = new Date(date.replace(/-/, "/"));
break;
case "number":
date = new Date(date);
break;
}
if (!date instanceof Date) return;
var dict = {
"yyyy": date.getFullYear(),
"M": date.getMonth() + 1,
"d": date.getDate(),
"H": date.getHours(),
"m": date.getMinutes(),
"s": date.getSeconds(),
"MM": ("" + (date.getMonth() + 101)).substr(1),
"dd": ("" + (date.getDate() + 100)).substr(1),
"HH": ("" + (date.getHours() + 100)).substr(1),
"mm": ("" + (date.getMinutes() + 100)).substr(1),
"ss": ("" + (date.getSeconds() + 100)).substr(1)
};
return format.replace(/(yyyy|MM?|dd?|HH?|ss?|mm?)/g, function() {
return dict[arguments[0]];
});
}
最后说明一下 我发的只是两个方法,是原文的,并无连贯性,并未做处理。
我说的重点是问题的思路和原因。
个人笔记,转载指定链接
最新文章
- 用 eric6 与 PyQt5 实现python的极速GUI编程(系列04)---- PyQt5自带教程:地址簿(address book)
- css中font-size的单位总结:px、em、pt
- json不转化值是null的字段
- (C#) System.BadImageFormatException: An attempt was made to load a program with an incorrect format.
- Yii通过控制台命令创建定时任务
- PHPCMS 2008 SQL注入漏洞
- GCOV 使用用例
- FMX相当于在界面上自己又做了一个小操作系统
- angular.js_$scope
- Hbase思维导图之数据存储
- Spring里的applicationContext.getBeansWithAnnotation
- flutter学习地址
- Spark函数式编程进阶
- threading的join功能
- poj3667 Hotel (线段树 区间合并)
- Python拾遗
- SQL Server Management Studio 键盘快捷键
- sicily 1500. Prime Gap
- html页面禁止选择复制剪切
- 安卓控件获取器uiautomatorviewer初体验:";unable to connect to the adb. check if adb is installed correctly";
热门文章
- PJzhang:从csdn到pipal密码分析工具
- @Conditional注解
- 【VS开发】字符,字节和编码
- wtforms 简单使用
- ssh远程连接的故障排查详解
- [转帖]ORA-00600-[kcratr_nab_less_than_odr]问题小记
- 在PostgreSQL中 pg_start_backup 做了什么?
- FFmpeg4.0笔记:采集系统声音
- CentOS7通过YUM安装NGINX稳定版本
- sklearn-GDBT