js 时间格式化 (兼容safari)
js 时间格式化,兼容IE8和safari浏览器。
function formatDate(date, fmt, near, type) {
var dateStr = date;
if (!dateStr || !fmt) {
return;
}
if (near && typeof date == 'string') {
if (!(dateStr - 0)) return;
if (!type || type == 'date') {
dateStr = dateStr.substr(0, 4) + '/' + dateStr.substr(4, 2) + '/' + dateStr.substr(6, 2);
} else if (type == 'month') {
dateStr = dateStr.substr(0, 4) + '/' + dateStr.substr(4, 2) + '/01'; // 兼容safari
}
}
if (typeof dateStr == 'string') {
dateStr = dateStr.replace(/-/g, "/"); // 兼容safari
}
var tempDate = new Date(dateStr);
if (tempDate == 'Invalid Date') return;
var o = {
"M+": tempDate.getMonth() + 1, //月份
"d+": tempDate.getDate(), //日
"h+": tempDate.getHours(), //小时
"m+": tempDate.getMinutes(), //分
"s+": tempDate.getSeconds(), //秒
"q+": Math.floor((tempDate.getMonth() + 3) / 3), //季度
"S": tempDate.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (tempDate.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
console.log(formatDate(new Date(), 'yyyy年MM月dd日')); // 2017年05月05日
console.log(formatDate('20180220', 'MM/dd', true)); // 02/20
也可以采用 Date.prototype.formatDate = function(date, fmt, near, type) {}这种扩展原型的方式,显得更优雅些。
js 时间格式化 (兼容safari)的更多相关文章
- js 时间格式化 兼容safari 苹果手机
export function formatTime (fmt, date) { date = new Date(date + '+08:00') // 兼容safari var o = { 'M+' ...
- js时间格式化函数,支持Unix时间戳
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- 时间戳显示为多少分钟前,多少天前的JS处理,JS时间格式化,时间戳的转换
var dateDiff = function (timestamp) { // 补全为13位 var arrTimestamp = (timestamp + '').split(''); for ( ...
- 表单序列化json字符串和js时间格式化
js时间格式化 new Date().format("时间格式") Date.prototype.format = function(fmt) { var o = { ...
- js时间格式化函数(兼容IOS)
* 时间格式化 * @param {Object} dateObj 时间对象 * @param {String} fmt 格式化字符串 */ dateFormat(dateObj, fmt) { le ...
- js时间格式化
const formatDate = timestamp => { const date = new Date(timestamp); const m = date.getMonth() + 1 ...
- js时间格式化(yy年MM月dd日 hh:mm)
//时间格式化 Date.prototype.format = function (format) { var o = { "M+": this.getMonth() + 1, / ...
- js 时间格式化 -- 时间加减实现
时间格式化的方法: Date.prototype.Format = function (fmt) { //author: meizz var o = { "M+": this.ge ...
- JS 时间格式化函数
//时间格式化函数 Date.prototype.format = function (format) { var o = { "M+": this.getMonth() + 1, ...
随机推荐
- EntityFrameworkCore 数据库生成与迁移
EntityFrameworkCore code first 中数据库不再自动生成,而要手动执行数据迁移相关命令生成. 由于云数据库的安全限制,没有开放公网数据库远程连接端口导致数据库生成命令无法执行 ...
- this小结
this 对象是在运行时基于函数的执行环境绑定的: 全局函数中, this 等于 window 函数被作为某个对象的方法调用时, this 等于那个对象 匿名函数的执行环境具有全局性, this 指向 ...
- Python——免费观看全网视频小程序
说明,这个小程序是基于网站“全民解析”,调用该网站的接口,实现数据传输观看视频,若该网站凉凉,则此程序凉凉. 开始之前的分析: 进入全民解析网站,我们首先查看一下网页的html代码,发现该站观看视频的 ...
- 2.6 Go 读取CSV
Go读取CSV文件,其内容被转换成字符串数组 package main import ( "encoding/csv" "fmt" "io/iouti ...
- vue,新手上路,基础,常见问题
1. 报这个错的 都是关键字问题,不要用关键字 做为组件的名字,不然肯定都会报错,如果找不到就一个字母的看,我就是 忘记改组件的名字导致报错,这个问题 改个名字就好,切记改全不然只有页面报错,文 ...
- Duilib总体框架
从GoogleCode上下载的duilib工程中附带的一副总体设计图(如下所示),可以先整体了解一下,有个初步的认识,对后续进一步深入了解学习会很有帮助. 通过设计图有了一个初步认识后,接下来开始进一 ...
- overload_protect_config.txt
overload_protection_switch=Y reject_uri_list= reject_request_percent=50 period_time=10 period_max_fa ...
- (转)expect命令脚本语言介绍及生产实践
原文:http://www.fblinux.com/?p=526 Expect介绍 expect是一个用来实现自动交互功能的软件套件,是用来实现自动和交互式任务程序进行通信,无需人的手工干预.比如SS ...
- python中函数参数传递的几种方法
转自 http://www.douban.com/note/13413855/ Python中函数参数的传递是通过“赋值”来传递的.但这条规则只回答了函数参数传递的“战略问题”,并没有回答“战术问题 ...
- OSI与TCP/IP协议区别
1 OSI参考模型 谈到网络不能不谈OSI参考模型,虽然OSI参考模型的实际应用意义不是很大,但其的确对于理解网络协议内部的运作很有帮助,也为我们学习网络协议提供了一个很好的参考.在现实网络世界里,T ...