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, ...
随机推荐
- js 面向对象 定时器 046
获取DOM对象补充 document.getElementsByTagName('div'); //获取的多个DOM对象 这种对象叫伪数组 如果想遍历此对象 通过for(var i=0; i < ...
- Mac显示隐藏的文件夹
方法一: 第一步:打开「终端」应用程序.第二步:输入如下命令:defaults write com.apple.finder AppleShowAllFiles -boolean true ; kil ...
- border.css(解决移动端1px问题)
由于某些机型分辨率过高,会导致1px变成2-多px像素的问题,引用bordercss解决 @charset "utf-8"; .border, .border-top, .bord ...
- TreeSet和TreeMap不能存放重复元素?能不能存放null?
问题一:本来认为TreeMap不能存放重复元素?其实并非如此: 其实一般情况下是不允许存放重复元素的,但是它并非这么死板,在一些情况下是可以存放重复元素的,存了又会有引入其他问题. 问题二:能不能存放 ...
- 页面多个 swiper 互补冲突
方法一:精简版 $(".swiper-container").each(function(){ $(this).swiper({ loop: true, initialSlide ...
- Git学习系列之经典的Git开发过程
前言 Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. 分布式相比于集中式的最大区别在于开发者可以提交到本地,每个开发者通过克隆(git clone),在本地机器上 ...
- Jquery中和ajax有关的方法
Jquery关于ajax有一系列的方法函数,单单知道$.ajax()显然是不够的,接下来我们对该系列的方法函数逐一研究下. ajaxComplete(callback).ajaxError(callb ...
- html中使用滚动条
1. 在html页面中使用滚动条,效果如下: 代码如下: <div style="height:auto !important;max-height:200px;overflow:sc ...
- [PY3]——内置数据结构(8)——解构与封装
### 解构的理解与用法 ### 解构是python很有特色的一个功能,被很多语言借鉴(例如ES6) # 元素按照顺序赋值给变量 In [31]: lst=list(range(5)) In [32] ...
- java中四种引用类型(对象的强、软、弱和虚引用)
对象的强.软.弱和虚引用在JDK 1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无法再使用这个对象.也就是说,只有对象处于可触及(reachable)状态,程序才能使用它.从JDK 1.2 ...