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, ...
随机推荐
- jquery ajax的getJSON使用
getJSON的定义和用法 通过 HTTP GET 请求载入 JSON 数据. 在 jQuery 1.2 中,您可以通过使用 JSONP 形式的回调函数来加载其他网域的 JSON 数据,如 " ...
- 超大图片显示,可任意缩放,移动,不用DiskLruCache
1.演示,代码 下载示例apk 下载项目 : https://gitee.com/xi/LImage.git 2.遇到的问题 想省内存,不太可能 只支持拖拽手势,不支持缩放相对简单,解码v ...
- a 标签的跳转属性
a 标签中调用js的几种方法 我们常用的在a标签中有点击事件:1. a href="JavaScript:js_method();" 这是我们平台上常用的方法,但是这种方法在 ...
- Scrapy框架学习(一)Scrapy框架介绍
Scrapy框架的架构图如上. Scrapy中的数据流由引擎控制,数据流的过程如下: 1.Engine打开一个网站,找到处理该网站的Spider,并向该Spider请求第一个要爬取得URL. 2.En ...
- NPM 与前端包管理
我们很清楚,前端资源及其依赖管理一直是 npm 的重度使用场景,同时这也一直是 Node.js 普及的重要推动力.但这类应用场景到底有多重度?这是一个很难回答的问题.这份 “npm 最常下载的包的清单 ...
- Firebird execute block 批处理
火鸟的批处理,效率好高,使用简单. execute block as declare variable i ; begin ) do begin :i = :i + ; insert into m_u ...
- Firebird Connection pool is full
今天在做Firebird V3.0.3 x64 版本内存测试,本地PC上,准备开启800个事务(保持不关闭),每个事务做些事,尽量不释放内存. 每次测试当事务数达到时,就提示Connection p ...
- 原创:微信小程序之MaterialDesign--input组件
作者:jeffer 来自:原文地址 主要通过input输入事件配合css的transform动态改变实现这种效果. 实际调试过程中,input组件bindinput事件触发后回调的detail对象,在 ...
- table中列复选框全选,再选 效果
<table class="table table-striped sortable table-bordered table-hover " id="zdnews ...
- Entity Framework6 with Visual Studio 2013 update3 for Oracle 11g
2014年7月的时候,写了一篇关于EF5 with visual studio 2010 for oracle 11g的博文 原文地址 :http://www.cnblogs.com/HouZhiHo ...