首先介绍一下我遇到的坑,找了几个关于字符串转时间的,他们都可以就我用的时候不行。

我的原因,我的字符串是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]];
});
}

最后说明一下 我发的只是两个方法,是原文的,并无连贯性,并未做处理。

我说的重点是问题的思路和原因。

个人笔记,转载指定链接

最新文章

  1. 用 eric6 与 PyQt5 实现python的极速GUI编程(系列04)---- PyQt5自带教程:地址簿(address book)
  2. css中font-size的单位总结:px、em、pt
  3. json不转化值是null的字段
  4. (C#) System.BadImageFormatException: An attempt was made to load a program with an incorrect format.
  5. Yii通过控制台命令创建定时任务
  6. PHPCMS 2008 SQL注入漏洞
  7. GCOV 使用用例
  8. FMX相当于在界面上自己又做了一个小操作系统
  9. angular.js_$scope
  10. Hbase思维导图之数据存储
  11. Spring里的applicationContext.getBeansWithAnnotation
  12. flutter学习地址
  13. Spark函数式编程进阶
  14. threading的join功能
  15. poj3667 Hotel (线段树 区间合并)
  16. Python拾遗
  17. SQL Server Management Studio 键盘快捷键
  18. sicily 1500. Prime Gap
  19. html页面禁止选择复制剪切
  20. 安卓控件获取器uiautomatorviewer初体验:"unable to connect to the adb. check if adb is installed correctly"

热门文章

  1. PJzhang:从csdn到pipal密码分析工具
  2. @Conditional注解
  3. 【VS开发】字符,字节和编码
  4. wtforms 简单使用
  5. ssh远程连接的故障排查详解
  6. [转帖]ORA-00600-[kcratr_nab_less_than_odr]问题小记
  7. 在PostgreSQL中 pg_start_backup 做了什么?
  8. FFmpeg4.0笔记:采集系统声音
  9. CentOS7通过YUM安装NGINX稳定版本
  10. sklearn-GDBT