js中格式化时间字符串
2014-10-24 16:10
225 查看
在javascript中,关于时间格式的转换。
可以将“2010-1-2” 转换为 “2010-01-02 00:00:00”
或者将“2010-1-2 2:13:6" 转换为 “2010-01-02 02:13:06”
第一种格式转换
第二种格式转换
可以将“2010-1-2” 转换为 “2010-01-02 00:00:00”
或者将“2010-1-2 2:13:6" 转换为 “2010-01-02 02:13:06”
第一种格式转换
<script> 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]]; }); } alert(formatDate("2010-04-30", "yyyy-MM-dd HH:mm:ss")); alert(formatDate("2010-4-29 1:50:00", "yyyy-MM-dd HH:mm:ss")); </script>
相关文章推荐
- js中格式化时间字符串
- js 定时跳转, 格式化字符串时间
- js格式化日期字符串进行时间比较
- js字符串格式化时间
- js扩展Date将时间格式化为"yyyy-MM-dd hh:mm:ss"格式的字符串
- js 将时间戳格式化为 yyyy-MM-dd HH:mm:ss 格式的字符串
- js中格式化时间字符串
- js将格式化后的时间拼接成字符串
- 时间字符串格式化 js
- js中格式化时间字符串
- 表单序列化json字符串和js时间格式化
- js时间格式化工具,时间戳格式化,字符串转时间戳
- js中格式化时间字符串
- 在js中对时间类型格式化字符串
- js中格式化时间字符串
- js强大的日期格式化,timestamp支持10位或13位的时间戳,或是时间字符串
- js中截取无特殊符号的字符串和取得特殊时间
- [收藏] C# 格式化时间字符串
- #时间格式化字符串详解
- C#格式化字符串,日期,时间,货币