📄 date.js
字号:
// Monday return String(dateObject.getDay() || 7); break; case "U": // week number of the current year as a decimal number, // starting with the first Sunday as the first day of the // first week return _(dojo.date.getWeekOfYear(dateObject)); break; case "V": // week number of the year (Monday as the first day of the // week) as a decimal number [01,53]. If the week containing // 1 January has four or more days in the new year, then it // is considered week 1. Otherwise, it is the last week of // the previous year, and the next week is week 1. return _(dojo.date.getIsoWeekOfYear(dateObject)); break; case "W": // week number of the current year as a decimal number, // starting with the first Monday as the first day of the // first week return _(dojo.date.getWeekOfYear(dateObject, 1)); break; case "w": // day of the week as a decimal, Sunday being 0 return String(dateObject.getDay()); break; case "x": // preferred date representation for the current locale // without the time break; case "X": // preferred date representation for the current locale // without the time break; case "y": // year as a decimal number without a century (range 00 to // 99) return _(dateObject.getFullYear()%100); break; case "Y": // year as a decimal number including the century return String(dateObject.getFullYear()); break; case "z": // time zone or name or abbreviation var timezoneOffset = dateObject.getTimezoneOffset(); return (timezoneOffset < 0 ? "-" : "+") + _(Math.floor(Math.abs(timezoneOffset)/60)) + ":" + _(Math.abs(timezoneOffset)%60); break; case "Z": // time zone or name or abbreviation return dojo.date.getTimezoneName(dateObject); break; case "%": return "%"; break; } } // parse the formatting string and construct the resulting string var string = ""; var i = 0, index = 0, switchCase; while ((index = format.indexOf("%", i)) != -1) { string += format.substring(i, index++); // inspect modifier flag switch (format.charAt(index++)) { case "_": // Pad a numeric result string with spaces. padChar = " "; break; case "-": // Do not pad a numeric result string. padChar = ""; break; case "0": // Pad a numeric result string with zeros. padChar = "0"; break; case "^": // Convert characters in result string to upper case. switchCase = "upper"; break; case "#": // Swap the case of the result string. switchCase = "swap"; break; default: // no modifer flag so decremenet the index padChar = null; index--; break; } // toggle case if a flag is set var property = $(format.charAt(index++)); if (switchCase == "upper" || (switchCase == "swap" && /[a-z]/.test(property))) { property = property.toUpperCase(); } else if (switchCase == "swap" && !/[a-z]/.test(property)) { property = property.toLowerCase(); } var swicthCase = null; string += property; i = index; } string += format.substring(i); return string;}/* compare and add ******************/dojo.date.compareTypes={ // summary // bitmask for comparison operations. DATE:1, TIME:2 };dojo.date.compare=function(/* Date */ dateA, /* Date */ dateB, /* int */ options){ // summary // Compare two date objects by date, time, or both. var dA=dateA; var dB=dateB||new Date(); var now=new Date(); var opt=options||(dojo.date.compareTypes.DATE|dojo.date.compareTypes.TIME); var d1=new Date( ((opt&dojo.date.compareTypes.DATE)?(dA.getFullYear()):now.getFullYear()), ((opt&dojo.date.compareTypes.DATE)?(dA.getMonth()):now.getMonth()), ((opt&dojo.date.compareTypes.DATE)?(dA.getDate()):now.getDate()), ((opt&dojo.date.compareTypes.TIME)?(dA.getHours()):0), ((opt&dojo.date.compareTypes.TIME)?(dA.getMinutes()):0), ((opt&dojo.date.compareTypes.TIME)?(dA.getSeconds()):0) ); var d2=new Date( ((opt&dojo.date.compareTypes.DATE)?(dB.getFullYear()):now.getFullYear()), ((opt&dojo.date.compareTypes.DATE)?(dB.getMonth()):now.getMonth()), ((opt&dojo.date.compareTypes.DATE)?(dB.getDate()):now.getDate()), ((opt&dojo.date.compareTypes.TIME)?(dB.getHours()):0), ((opt&dojo.date.compareTypes.TIME)?(dB.getMinutes()):0), ((opt&dojo.date.compareTypes.TIME)?(dB.getSeconds()):0) ); if(d1.valueOf()>d2.valueOf()){ return 1; // int } if(d1.valueOf()<d2.valueOf()){ return -1; // int } return 0; // int}dojo.date.dateParts={ // summary // constants for use in dojo.date.add YEAR:0, MONTH:1, DAY:2, HOUR:3, MINUTE:4, SECOND:5, MILLISECOND:6 };dojo.date.add=function(/* Date */ d, /* dojo.date.dateParts */ unit, /* int */ amount){ var n=(amount)?amount:1; var v; switch(unit){ case dojo.date.dateParts.YEAR:{ v=new Date(d.getFullYear()+n, d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds()); break; } case dojo.date.dateParts.MONTH:{ v=new Date(d.getFullYear(), d.getMonth()+n, d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds()); break; } case dojo.date.dateParts.HOUR:{ v=new Date(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours()+n, d.getMinutes(), d.getSeconds(), d.getMilliseconds()); break; } case dojo.date.dateParts.MINUTE:{ v=new Date(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes()+n, d.getSeconds(), d.getMilliseconds()); break; } case dojo.date.dateParts.SECOND:{ v=new Date(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds()+n, d.getMilliseconds()); break; } case dojo.date.dateParts.MILLISECOND:{ v=new Date(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds()+n); break; } default:{ v=new Date(d.getFullYear(), d.getMonth(), d.getDate()+n, d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds()); } }; return v; // Date};/* Deprecated *************/dojo.date.toString = function(date, format){ dojo.deprecated("dojo.date.toString", "use dojo.date.format instead", "0.4"); if (format.indexOf("#d") > -1) { format = format.replace(/#dddd/g, dojo.date.getDayOfWeekName(date)); format = format.replace(/#ddd/g, dojo.date.getShortDayOfWeekName(date)); format = format.replace(/#dd/g, (date.getDate().toString().length==1?"0":"")+date.getDate()); format = format.replace(/#d/g, date.getDate()); } if (format.indexOf("#M") > -1) { format = format.replace(/#MMMM/g, dojo.date.getMonthName(date)); format = format.replace(/#MMM/g, dojo.date.getShortMonthName(date)); format = format.replace(/#MM/g, ((date.getMonth()+1).toString().length==1?"0":"")+(date.getMonth()+1)); format = format.replace(/#M/g, date.getMonth() + 1); } if (format.indexOf("#y") > -1) { var fullYear = date.getFullYear().toString(); format = format.replace(/#yyyy/g, fullYear); format = format.replace(/#yy/g, fullYear.substring(2)); format = format.replace(/#y/g, fullYear.substring(3)); } // Return if only date needed; if (format.indexOf("#") == -1) { return format; } if (format.indexOf("#h") > -1) { var hours = date.getHours(); hours = (hours > 12 ? hours - 12 : (hours == 0) ? 12 : hours); format = format.replace(/#hh/g, (hours.toString().length==1?"0":"")+hours); format = format.replace(/#h/g, hours); } if (format.indexOf("#H") > -1) { format = format.replace(/#HH/g, (date.getHours().toString().length==1?"0":"")+date.getHours()); format = format.replace(/#H/g, date.getHours()); } if (format.indexOf("#m") > -1) { format = format.replace(/#mm/g, (date.getMinutes().toString().length==1?"0":"")+date.getMinutes()); format = format.replace(/#m/g, date.getMinutes()); } if (format.indexOf("#s") > -1) { format = format.replace(/#ss/g, (date.getSeconds().toString().length==1?"0":"")+date.getSeconds()); format = format.replace(/#s/g, date.getSeconds()); } if (format.indexOf("#T") > -1) { format = format.replace(/#TT/g, date.getHours() >= 12 ? "PM" : "AM"); format = format.replace(/#T/g, date.getHours() >= 12 ? "P" : "A"); } if (format.indexOf("#t") > -1) { format = format.replace(/#tt/g, date.getHours() >= 12 ? "pm" : "am"); format = format.replace(/#t/g, date.getHours() >= 12 ? "p" : "a"); } return format; }dojo.date.daysInMonth = function (month, year) { dojo.deprecated("daysInMonth(month, year)", "replaced by getDaysInMonth(dateObject)", "0.4"); return dojo.date.getDaysInMonth(new Date(year, month, 1));}/** * * Returns a string of the date in the version "January 1, 2004" * * @param date The date object */dojo.date.toLongDateString = function(date) { dojo.deprecated("dojo.date.toLongDateString", 'use dojo.date.format(date, "%B %e, %Y") instead', "0.4"); return dojo.date.format(date, "%B %e, %Y")}/** * * Returns a string of the date in the version "Jan 1, 2004" * * @param date The date object */dojo.date.toShortDateString = function(date) { dojo.deprecated("dojo.date.toShortDateString", 'use dojo.date.format(date, "%b %e, %Y") instead', "0.4"); return dojo.date.format(date, "%b %e, %Y");}/** * * Returns military formatted time * * @param date the date object */dojo.date.toMilitaryTimeString = function(date){ dojo.deprecated("dojo.date.toMilitaryTimeString", 'use dojo.date.format(date, "%T")', "0.4"); return dojo.date.format(date, "%T");}/** * * Returns a string of the date relative to the current date. * * @param date The date object * * Example returns: * - "1 minute ago" * - "4 minutes ago" * - "Yesterday" * - "2 days ago" */dojo.date.toRelativeString = function(date) { var now = new Date(); var diff = (now - date) / 1000; var end = " ago"; var future = false; if(diff < 0) { future = true; end = " from now"; diff = -diff; } if(diff < 60) { diff = Math.round(diff); return diff + " second" + (diff == 1 ? "" : "s") + end; } else if(diff < 3600) { diff = Math.round(diff/60); return diff + " minute" + (diff == 1 ? "" : "s") + end; } else if(diff < 3600*24 && date.getDay() == now.getDay()) { diff = Math.round(diff/3600); return diff + " hour" + (diff == 1 ? "" : "s") + end; } else if(diff < 3600*24*7) { diff = Math.round(diff/(3600*24)); if(diff == 1) { return future ? "Tomorrow" : "Yesterday"; } else { return diff + " days" + end; } } else { return dojo.date.toShortDateString(date); }}/** * Retrieves the day of the week the Date is set to. * * @return The day of the week */dojo.date.getDayOfWeekName = function (date) { dojo.deprecated("dojo.date.getDayOfWeekName", "use dojo.date.getDayName instead", "0.4"); return dojo.date.days[date.getDay()];}/** * Retrieves the short day of the week name the Date is set to. * * @return The short day of the week name */dojo.date.getShortDayOfWeekName = function (date) { dojo.deprecated("dojo.date.getShortDayOfWeekName", "use dojo.date.getDayShortName instead", "0.4"); return dojo.date.shortDays[date.getDay()];}/** * Retrieves the short month name the Date is set to. * * @return The short month name */dojo.date.getShortMonthName = function (date) { dojo.deprecated("dojo.date.getShortMonthName", "use dojo.date.getMonthShortName instead", "0.4"); return dojo.date.shortMonths[date.getMonth()];}/** * Convert a Date to a SQL string, optionally ignoring the HH:MM:SS portion of the Date */dojo.date.toSql = function(date, noTime) { return dojo.date.format(date, "%F" + !noTime ? " %T" : "");}/** * Convert a SQL date string to a JavaScript Date object */dojo.date.fromSql = function(sqlDate) { var parts = sqlDate.split(/[\- :]/g); while(parts.length < 6) { parts.push(0); } return new Date(parts[0], (parseInt(parts[1],10)-1), parts[2], parts[3], parts[4], parts[5]);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -