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, ...
随机推荐
- mkvirtualenv创建python虚拟环境
为什么要创建虚拟环境呢? 如果在我们本地开发好几个项目,每个项目又用到很多第三方插件,如果没没用创建虚拟环境,那么本地项目插件就会很多很乱,如果有虚拟环境就不一样了 虚拟环境安装 pip instal ...
- Removable Storage Devices文件夹删除方法
Windows10的桌面上出现了名为“Removable Storage Devices”的文件夹删除方法 比较莫名奇妙,突然桌面上出现了名为“Removable Storage Devices”的文 ...
- jQuery示例 | 分级菜单
<!DOCTYPE html> Title .header{ background-color: black; color: wheat; } .content{ min-height: ...
- Ubuntu 18.04上搭建FTP服务器
1.准备工作需要安装并运行的Ubuntu Server 18.04系统.当然还需要一个具有sudo权限的账号. 2.安装VSFTPVSFTP程序位于标准存储库中,因此可以使用单个命令删除安装.打开终端 ...
- springboot实现服务器端消息推送(H5原生支持)
随着互联网的发展,传统的HTTP协议已经很难满足Web应用日益复杂的需求了.近年来,随着HTML5的诞生,WebSocket协议被提出,它实现了浏览器与服务器的全双工通信,扩展了浏览器与服务端的通信功 ...
- Git学习系列之为什么选择Git?
为什么选择Git? 流行的软件版本开源管理软件,有CVS.SVN.GIT版本管理工具,Git的优势在哪里呢? Git 和 CVS.SVN不同,是一个分布式的源代码管理工具,它很强,也很快,Linux内 ...
- 案例16-validate自定义校验规则校验用户名是否存在
1 知识点 2 register.jsp代码 注意自定义校验规则的时候,提交必须是同步的方式. <%@ page language="java" contentType=&q ...
- Android规划周期任务
问题:应用总要周期性的执行某项任务,例如检查服务器上的更新或者提醒用户做某些事情. 解决方案:用AlarmManager来管理和执行任务.AlarmManager可用于计划未来的单次或重复操作,甚至在 ...
- Python读取文件编码及内容
Python读取文件编码及内容 最近做一个项目,需要读取文件内容,但是文件的编码方式有可能都不一样.有的使用GBK,有的使用UTF8.所以在不正确读取的时候会出现如下错误: UnicodeDecode ...
- Guava学习笔记:Optional优雅的使用null
在我们学习和使用Guava的Optional之前,我们需要来了解一下Java中null.因为,只有我们深入的了解了null的相关知识,我们才能更加深入体会领悟到Guava的Optional设计和使用上 ...