zoomla
Version:
16年专业研发|中文alexa排名第一的CMS品牌-基于dotNET core、功能强大,集成站群、微信开发、小程序与ERP及OA办公系统,支持国际语言和多民族语言,世界五百强与大型门户专用高端网站内核CMS系统
96 lines (92 loc) • 4.34 kB
JavaScript
//moment.js
//标准格式为 yyyy-MM-dd HH:mm:ss 不支持到毫秒级
var DateHelper = {};
//转化秒为时间,返回模型
DateHelper.SecondToTime = function (time) {
var model = this.getModel();
if (!time || null == time || "" == time) return model;
model.day = parseInt(time / (60 * 60 * 24));
if (model.day > 0) { time = time - ((60 * 60 * 24) * model.day); }
model.hour = parseInt(time / (60 * 60));
if (model.hour > 0) { time = time - ((60 * 60) * model.hour); }
model.minute = parseInt(time / 60);
if (model.minute > 0) { time = time - (60 * model.minute); }
model.second = time;
return model;
}
DateHelper.getDate = function (formatStr) {
if (!formatStr) { formatStr = "yyyy-MM-dd HH:mm:ss"; }
var myDate = new Date();
return DateHelper.toString(myDate,formatStr);
}
//时间间隔,返回秒,非负数
DateHelper.getInterval = function (sdate, edate) {
var ref = this;
var startTime = new Date(Date.parse(sdate.replace(/-/g, "/"))).getTime();
var endTime = new Date(Date.parse(edate.replace(/-/g, "/"))).getTime();
var second = Math.abs((startTime - endTime)) / (1000);//* 60 * 60 * 24
return second;
}
//1980-01-12 00:00:00||1980/01/12 00:00:00||1980-01-12T00:00:00
DateHelper.dateToModel = function (str) {
var ref = this;
str = str.replace(/\//ig, "-").replace("T", " ");
var model = ref.getModel();
var date = str.split(' ')[0];
var time = str.split(' ')[1];
model.year = date.split('-')[0];
model.month = date.split('-')[1];
model.day = date.split('-')[2];
model.hour = time.split(':')[0];
model.minute = time.split(':')[1];
model.second = time.split(':')[2];
return model;
}
//模型即可用于描述时间,也可用于描述间隔
DateHelper.getModel = function () {
var model = { year: 0, month: 0, day: 0, hour: 0, minute: 0, second: 0 };
model.isHasTime = function () {
return (this.year > 0 || this.month > 0 || this.day > 0 || this.hour > 0 || this.minute > 0 || this.second > 0);
}
model.toString = function () {
var result = "";
if (model.year > 0) { result += model.year + "年 "; }
if (model.month > 0) { result += model.month + "个月 "; }
if (model.day > 0) { result += model.day + "天 "; }
if (model.hour > 0) { result += model.hour + "小时 "; }
if (model.minute > 0) { result += model.minute + "分钟 "; }
return result;
}
return model;
}
//获取指定日期的月份有多少天
DateHelper.getMonthDays = function (year, month) {
var monthStartDate = new Date(year, (month-1), 1);
var monthEndDate = new Date(year, month, 1);
var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
return days;
}
DateHelper.toString = function (myDate, formatStr) {
var Week = ['日', '一', '二', '三', '四', '五', '六'];
var str = formatStr.replace(/yyyy|YYYY/, myDate.getFullYear());
str = str.replace(/yy|YY/, (myDate.getYear() % 100) > 9 ? (myDate.getYear() % 100).toString() : '0' + (myDate.getYear() % 100));
var month = (myDate.getMonth() + 1); if (month < 10) { month = "0" + month; }
str = str.replace(/MM/, month);
str = str.replace(/M/g, month);
str = str.replace(/w|W/g, Week[myDate.getDay()]);
str = str.replace(/dd|DD/, myDate.getDate() > 9 ? myDate.getDate().toString() : '0' + myDate.getDate());
str = str.replace(/d|D/g, myDate.getDate());
str = str.replace(/hh|HH/, myDate.getHours() > 9 ? myDate.getHours().toString() : '0' + myDate.getHours());
str = str.replace(/h|H/g, myDate.getHours());
str = str.replace(/mm/, myDate.getMinutes() > 9 ? myDate.getMinutes().toString() : '0' + myDate.getMinutes());
str = str.replace(/m/g, myDate.getMinutes());
str = str.replace(/ss|SS/, myDate.getSeconds() > 9 ? myDate.getSeconds().toString() : '0' + myDate.getSeconds());
str = str.replace(/s|S/g, myDate.getSeconds());
return str;
}
//返回标准的时间格式,用于避免IE下不兼容问题
DateHelper.pure = function (time) {
if (!time) { console.log(time + "时间格式不正确"); return ""; }
time = time.replace("T", " ").replace(/-/g, "/").split('.')[0];
return time;
}