json.date.js时间相关-创新互联
为山海关等地区用户提供了全套网页设计制作服务,及山海关网站建设行业解决方案。主营业务为网站制作、网站建设、山海关网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!>function ConvertJSONDateToJSDateObject(jsondate) {
var date = new Date(parseInt(jsondate.replace("/Date(", "").replace(")/", ""), 10));
return date;
}
var _toIsoDate = Date.prototype.toISOString;
//借用kendo.toString做出Unspecified Kind的ISO8601格式//Date.prototype.toISOString = function () {// return kendo.toString(this, "yyyy-MM-ddTHH:mm:ss");//};var newdate = null;
function getToDay() {
var now = new Date();
var nowYear = now.getFullYear();
var nowMonth = now.getMonth();
var nowDate = now.getDate();
newdate= new Date(nowYear, nowMonth, nowDate);
nowMonth= doHandleMonth(nowMonth + 1);
nowDate= doHandleMonth(nowDate);
return nowYear + "-" + nowMonth + "-" + nowDate;
}
function doHandleMonth(month) {
if (month.toString().length == 1) {
month= "0" + month;
}
return month;
}
function getYesterDay() {
var newtimems = newdate.getTime() - (24 * 60 * 60 * 1000);
var yesd = new Date(newtimems);
var yesYear = yesd.getFullYear();
var yesMonth = yesd.getMonth();
var yesDate = yesd.getDate();
yesMonth= doHandleMonth(yesMonth + 1);
yesDate= doHandleMonth(yesDate);
return yesYear + "-" + yesMonth + "-" + yesDate;
}
function getDate(jsondate) {
var date = ConvertJSONDateToJSDateObject(jsondate);
var year = date.getFullYear();
var month = date.getMonth() + 1;
month= month < 10 ? "0" + month : month;
var day = date.getDate();
day= day < 10 ? "0" + day : day;
return year + "-" + month + "-" + day;
}
function getDateTime(jsondate) {
var date = ConvertJSONDateToJSDateObject(jsondate);
var year = date.getFullYear();
var month = date.getMonth() + 1;
//month = month < 10 ? "0" + month : month; var day = date.getDate();
//day = day < 10 ? "0" + day : day; var hh = date.getHours();
//hh = hh < 10 ? "0" + hh : hh; var mm = date.getMinutes();
mm= mm < 10 ? "0" + mm : mm;
var ss = date.getSeconds();
ss= ss < 10 ? "0" + ss : ss;
return year + "/" + month + "/" + day + " " + hh + ":" + mm + ":" + ss;
}
function getFormatDateTime(jsondate, strformatdatetime) {
var date = ConvertJSONDateToJSDateObject(jsondate);
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hh = date.getHours();
var mm = date.getMinutes();
mm= mm < 10 ? "0" + mm : mm;
var ss = date.getSeconds();
ss= ss < 10 ? "0" + ss : ss;
var newdatetime = strformatdatetime.replace("{yyyy}", year);
newdatetime= newdatetime.replace("{m}", month);
newdatetime= newdatetime.replace("{d}", day);
newdatetime= newdatetime.replace("{hh}", hh);
newdatetime= newdatetime.replace("{mm}", mm);
newdatetime= newdatetime.replace("{ss}", ss);
return newdatetime;
}
function getShortTime(jsondate) {
var date = ConvertJSONDateToJSDateObject(jsondate);
var hh = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
var mm = date.getMinutes() == 0 ? "00" : date.getMinutes();
return hh + ":" + mm ;
}
function getShortHour(jsondate) {
var date = ConvertJSONDateToJSDateObject(jsondate);
var hh = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
//var mm = date.getMinutes() == 0 ? "00" : date.getMinutes(); return hh + ":00";
}
//聚合算法function dateDiff(date1, date2) {
var type1 = typeof date1, type2 = typeof date2;
if (type1 == 'string')
date1= stringToTime(date1);
else if (date1.getTime)
date1= date1.getTime();
if (type2 == 'string')
date2= stringToTime(date2);
else if (date2.getTime)
date2= date2.getTime();
return (date1 - date2) / (1000 * 60 * 60 * 24); //结果是小时}
//字符串转成Time(dateDiff)所需方法function stringToTime(string) {
var f = string.split(' ', 2);
var d = (f[0] ? f[0] : '').split('-', 3);
var t = (f[1] ? f[1] : '').split(':', 3);
return (new Date(
parseInt(d[0], 10) || null,
(parseInt(d[1], 10) || 1) - 1,
parseInt(d[2], 10) || null,
parseInt(t[0], 10) || null,
parseInt(t[1], 10) || null,
parseInt(t[2], 10) || null
)).getTime();
}
// 增加天function AddDays(date, value) {
date.setDate(date.getDate()+ value);
}
// 增加月function AddMonths(date, value) {
date.setMonth(date.getMonth()+ value);
}
// 增加年function AddYears(date, value) {
date.setFullYear(date.getFullYear()+ value);
}
// 是否为今天function IsToday(date) {
return IsDateEquals(date, new Date());
}
// 是否为明天function IsTmorm(date) {
var today = new Date();
AddDays(today,1);
return IsDateEquals(date, today);
}
// 是否为当月function IsThisMonth(date) {
return IsMonthEquals(date, new Date());
}
// 两个日期的年是否相等function IsMonthEquals(date1, date2) {
return date1.getMonth() == date2.getMonth() && date1.getFullYear() == date2.getFullYear();
}
// 判断日期是否相等function IsDateEquals(date1, date2) {
return date1.getDate() == date2.getDate() && IsMonthEquals(date1, date2);
}
// 返回某个日期对应的月份的天数function GetMonthDayCount(date) {
switch (date.getMonth() + 1) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
return 31;
case 4: case 6: case 9: case 11:
return 30;
}
//feb: date = new Date(date);
var lastd = 28;
date.setDate(29);
while (date.getMonth() == 1) {
lastd++;
AddDays(date,1);
}
return lastd;
}
// 返回两位数的年份function GetHarfYear(date) {
var v = date.getYear();
if (v > 9) return v.toString();
return "0" + v;
}
// 返回月份(修正为两位数)function GetFullMonth(date) {
var v = date.getMonth() + 1;
if (v > 9) return v.toString();
return "0" + v;
}
// 返回日 (修正为两位数)function GetFullDate(date) {
var v = date.getDate();
if (v > 9) return v.toString();
return "0" + v;
}
// 返回时 (修正为两位数)function GetFullHour(date) {
var v = date.getHours();
if (v > 9) return v.toString();
return "0" + v;
}
// 返回分 (修正为两位数)function GetFullMinte(date) {
var v = date.getMinutes();
if (v > 9) return v.toString();
return "0" + v;
}
// 返回秒 (修正为两位数)function GetFullSec(date) {
var v = date.getSeconds();
if (v > 9) return v.toString();
return "0" + v;
}
// 替换字符串function Replace(str, from, to) {
return str.split(from).join(to);
}
// 格式化日期的表示function FormatDate(date, str) {
str= Replace(str, "yyyy", date.getFullYear());
str= Replace(str, "MM", GetFullMonth(date));
str= Replace(str, "dd", GetFullDate(date));
str= Replace(str, "yy", GetHarfYear(date));
str= Replace(str, "M", date.getMonth() + 1);
str= Replace(str, "d", date.getDate());
return str;
}
// 统一日期格式function ConvertDate(str) {
str= (str + "").replace(/^s*/g, "").replace(/s*$/g, ""); // 去除前后的空白 var d;
if (/^[0-9]{8}$/.test(str)) // 20040226 -> 2004-02-26 {
d= new Date(new Number(str.substr(0, 4)), new Number(str.substr(4, 2)) - 1, new Number(str.substr(6, 2)));
if (d.getTime()) return d;
}
d= new Date(str);
if (d.getTime()) return d;
d= new Date(Replace(str, "-", "/"));
if (d.getTime()) return d;
return null;
}
//获取星期function GetWeek(date)
{
var week;
if (date.getDay() == 0) week = "日";
if (date.getDay() == 1) week = "一";
if (date.getDay() == 2) week = "二";
if (date.getDay() == 3) week = "三";
if (date.getDay() == 4) week = "四";
if (date.getDay() == 5) week = "五";
if (date.getDay() == 6) week = "六";
return week;
}
当前题目:json.date.js时间相关-创新互联
分享链接:http://lswzjz.com/article/ppogi.html