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, ...
随机推荐
- Python用于http/https接口自动化
本接口自动化框架主要用到的类: 1. unittest:组织测试用例 2. requests:http/https请求 3. HTMLTestRunner:生成测试报告 4. Dingtalkchat ...
- SpringCloud---声明式服务调用---Spring Cloud Feign
1.概述 1.1 Spring Cloud Ribbon.Spring Cloud Hystrix的使用几乎是同时出现的,Spring Cloud提供了一个更高层次的封装这2个工具类框架:Spring ...
- golang io.ReadFull
buf := make([]byte, 10, 10) file, _ := os.Open("./data.txt") n, err := io.ReadFull(file, b ...
- JAVA学习3:Eclipse中集成Tomcat
问题: 很多时候在Eclipse中启动Tmocat后,不能访问本机的localhost:8080主页,并且其他项目也不能访问. 原因: 打开Tomcat下的webapp后也找补到项目目录,这是因为Ec ...
- springboot实现服务器端消息推送(H5原生支持)
随着互联网的发展,传统的HTTP协议已经很难满足Web应用日益复杂的需求了.近年来,随着HTML5的诞生,WebSocket协议被提出,它实现了浏览器与服务器的全双工通信,扩展了浏览器与服务端的通信功 ...
- 使用jsp完成商品列表的动态显示
1数据库准备工作 1创建数据库 2 创建product表 代码如下: CREATE TABLE `product` ( `pid` ) NOT NULL, `pname` ) DEFAULT NULL ...
- c++ 网络编程(一)TCP/UDP windows/linux 下入门级socket通信 客户端与服务端交互代码
原文作者:aircraft 原文地址:https://www.cnblogs.com/DOMLX/p/9601511.html c++ 网络编程(一)TCP/UDP 入门级客户端与服务端交互代码 网 ...
- idea中运行maven安装jar包到本地仓库跳过test
- Bootstrap使用模态框modal实现表单提交弹出框
Bootstrap 模态框(Modal)插件 模态框(Modal)是覆盖在父窗体上的子窗体.通常,目的是显示来自一个单独的源的内容,可以在不离开父窗体的情况下有一些互动.子窗体可提供信息.交互等.如果 ...
- 实体类的状态与Hibernate缓存
一.Hibernate中实体类的三种状态 1.瞬时态 该状态下实体类对象的id属性没有值,该对象和session也没有关系. 实例: UserEntity user = new UserEntity( ...