/* 
* Date Format 1.2.3 
* (c) 2007-2009 Steven Levithan 
* MIT license 
* 
* Includes enhancements by Scott Trenda 
* and Kris Kowal 
* 
* Accepts a date, a mask, or a date and a mask. 
* Returns a formatted version of the given date. 
* The date defaults to the current date/time. 
* The mask defaults to dateFormat.masks.default. 
*/ 

var dateFormat = function () { 
var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])?|[LloSZ]|"[^"]*"|'[^']*'/g, 
timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[- ]\d{4})?)\b/g, 
timezoneClip = /[^- \dA-Z]/g, 
pad = function (val, len) { 
val = String(val); 
len = len || 2; 
while (val.length < len) val = "0"   val; return val; }; // Regexes and supporting functions are cached through closure return function (date, mask, utc) { var dF = dateFormat; // You can't provide utc if you skip other args (use the "UTC:" mask prefix) if (arguments.length == 1 && Object.prototype.toString.call(date) == "[object String]" && !/\d/.test(date)) { mask = date; date = undefined; } // Passing date through Date applies Date.parse, if necessary date = date ? new Date(date) : new Date; if (isNaN(date)) throw SyntaxError("invalid date"); mask = String(dF.masks[mask] || mask || dF.masks["default"]); // Allow setting the utc argument via the mask if (mask.slice(0, 4) == "UTC:") { mask = mask.slice(4); utc = true; } var _ = utc ? "getUTC" : "get", d = date[_   "Date"](), D = date[_   "Day"](), m = date[_   "Month"](), y = date[_   "FullYear"](), H = date[_   "Hours"](), M = date[_   "Minutes"](), s = date[_   "Seconds"](), L = date[_   "Milliseconds"](), o = utc ? 0 : date.getTimezoneOffset(), flags = { d: d, dd: pad(d), ddd: dF.i18n.dayNames[D], dddd: dF.i18n.dayNames[D   7], m: m   1, mm: pad(m   1), mmm: dF.i18n.monthNames[m], mmmm: dF.i18n.monthNames[m   12], yy: String(y).slice(2), yyyy: y, h: H % 12 || 12, hh: pad(H % 12 || 12), H: H, HH: pad(H), M: M, MM: pad(M), s: s, ss: pad(s), l: pad(L, 3), L: pad(L > 99 ? Math.round(L / 10) : L), 
t: H < 12 ? "a" : "p", 
tt: H < 12 ? "am" : "pm", 
T: H < 12 ? "A" : "P", 
TT: H < 12 ? "AM" : "PM", Z: utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""), o: (o > 0 ? "-" : " ")   pad(Math.floor(Math.abs(o) / 60) * 100   Math.abs(o) % 60, 4), 
S: ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10] 
}; 

return mask.replace(token, function ([content]) { 
return [content] in flags ? flags[[content]] : [content].slice(1, [content].length - 1); 
}); 
}; 
}(); 

// Some common format strings 
dateFormat.masks = { 
"default": "ddd mmm dd yyyy HH:MM:ss", 
shortDate: "m/d/yy", 
mediumDate: "mmm d, yyyy", 
longDate: "mmmm d, yyyy", 
fullDate: "dddd, mmmm d, yyyy", 
shortTime: "h:MM TT", 
mediumTime: "h:MM:ss TT", 
longTime: "h:MM:ss TT Z", 
isoDate: "yyyy-mm-dd", 
isoTime: "HH:MM:ss", 
isoDateTime: "yyyy-mm-dd'T'HH:MM:ss", 
isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'" 
}; 

// Internationalization strings 
dateFormat.i18n = { 
dayNames: [ 
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", 
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" 
], 
monthNames: [ 
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 
"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" 
] 
}; 

// For convenience... 
Date.prototype.format = function (mask, utc) { 
return dateFormat(this, mask, utc); 
};

用法:
var now = new Date(); 

now.format("m/dd/yy"); 
// Returns, e.g., 6/09/07 

// Can also be used as a standalone function 
dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT"); 
// Saturday, June 9th, 2007, 5:46:21 PM 

// You can use one of several named masks 
now.format("isoDateTime"); 
// 2007-06-09T17:46:21 

// ...Or add your own 
dateFormat.masks.hammerTime = 'HH:MM! "Can\'t touch this!"'; 
now.format("hammerTime"); 
// 17:46! Can't touch this! 

// When using the standalone dateFormat function, 
// you can also provide the date as a string 
dateFormat("Jun 9 2007", "fullDate"); 
// Saturday, June 9, 2007 

// Note that if you don't include the mask argument, 
// dateFormat.masks.default is used 
now.format(); 
// Sat Jun 09 2007 17:46:21 

// And if you don't include the date argument, 
// the current date and time is used 
dateFormat(); 
// Sat Jun 09 2007 17:46:22 

// You can also skip the date argument (as long as your mask doesn't 
// contain any numbers), in which case the current date/time is used 
dateFormat("longTime"); 
// 5:46:22 PM EST 

// And finally, you can convert local time to UTC time. Either pass in 
// true as an additional argument (no argument skipping allowed in this case): 
dateFormat(now, "longTime", true); 
now.format("longTime", true); 
// Both lines return, e.g., 10:46:21 PM UTC 

// ...Or add the prefix "UTC:" to your mask. 
now.format("UTC:h:MM:ss TT Z"); 
// 10:46:21 PM UTC

Mask Description
d Day of the month as digits; no leading zero for single-digit days.
dd Day of the month as digits; leading zero for single-digit days.
ddd Day of the week as a three-letter abbreviation.
dddd Day of the week as its full name.
m Month as digits; no leading zero for single-digit months.
mm Month as digits; leading zero for single-digit months.
mmm Month as a three-letter abbreviation.
mmmm Month as its full name.
yy Year as last two digits; leading zero for years less than 10.
yyyy Year represented by four digits.
h Hours; no leading zero for single-digit hours (12-hour clock).
hh Hours; leading zero for single-digit hours (12-hour clock).
H Hours; no leading zero for single-digit hours (24-hour clock).
HH Hours; leading zero for single-digit hours (24-hour clock).
M Minutes; no leading zero for single-digit minutes.
Uppercase M unlike CF timeFormat's m to avoid conflict with months.
MM Minutes; leading zero for single-digit minutes.
Uppercase MM unlike CF timeFormat's mm to avoid conflict with months.
s Seconds; no leading zero for single-digit seconds.
ss Seconds; leading zero for single-digit seconds.
l or L Milliseconds. l gives 3 digits. L gives 2 digits.
t Lowercase, single-character time marker string: a or p.
No equivalent in CF.
tt Lowercase, two-character time marker string: am or pm.
No equivalent in CF.
T Uppercase, single-character time marker string: A or P.
Uppercase T unlike CF's t to allow for user-specified casing.
TT Uppercase, two-character time marker string: AM or PM.
Uppercase TT unlike CF's tt to allow for user-specified casing.
Z US timezone abbreviation, e.g. EST or MDT. With non-US timezones or in the Opera browser, the GMT/UTC offset is returned, e.g. GMT-0500
No equivalent in CF.
o GMT/UTC timezone offset, e.g. -0500 or 0230.
No equivalent in CF.
S The date's ordinal suffix (st, nd, rd, or th). Works well with d.
No equivalent in CF.
'…' or "…" Literal character sequence. Surrounding quotes are removed.
No equivalent in CF.
UTC: Must be the first four characters of the mask. Converts the date from local time to UTC/GMT/Zulu time before applying the mask. The “UTC:” prefix is removed.
No equivalent in CF.
Name Mask Example
default ddd mmm dd yyyy HH:MM:ss Sat Jun 09 2007 17:46:21
shortDate m/d/yy 6/9/07
mediumDate mmm d, yyyy Jun 9, 2007
longDate mmmm d, yyyy June 9, 2007
fullDate dddd, mmmm d, yyyy Saturday, June 9, 2007
shortTime h:MM TT 5:46 PM
mediumTime h:MM:ss TT 5:46:21 PM
longTime h:MM:ss TT Z 5:46:21 PM EST
isoDate yyyy-mm-dd 2007-06-09
isoTime HH:MM:ss 17:46:21
isoDateTime yyyy-mm-dd'T'HH:MM:ss 2007-06-09T17:46:21
isoUtcDateTime UTC:yyyy-mm-dd'T'HH:MM:ss'Z' 2007-06-09T22:46:21Z

Date对象格式化函数代码的更多相关文章

  1. 对于NSManagedObject,Xcode 9构建了Date vs NSDate的问题

    Xcode9为模拟器与设备中的实体的Date类型属性生成不同的代码.我在coredata中将类设置为类别/扩展名下的codegen功能.直到Xcode8.3(最新)它一切正常.下面是Xcode9为属性自动生成的代码–在设备上:–和,在模拟器上:–有谁遇到过这个问题?对于一个有50个成员的项目来解决这个问题的最佳解决方案是什么,直到Xcode更新修复它?

  2. iOS兼容输入类型日期 – 设置最小值最大值

    我试图在UIWebViewiOS应用程序中使用jQueryMobile设置日期,值设置正确但最小和最大属性日期设置不起作用.和当我在模拟器上运行它时,当选择日期字段时,日期选择器可见,但未设置最小,最大日期.解决方法模拟器的safari不支持设置输入类型=“日期”的最小值和最大值.您可以通过导航到thissite并尝试控制来测试它.它(可能)可以在桌面浏览器上运行,但不会在模拟器的浏览器或UIWebView中运行.

  3. ios – 设置NSDataDetector的上下文日期

    假设今天是2014年1月20日.如果我使用NSDataDetector从“明天下午4点”字符串中提取日期,我将得到2014-01-21T16:00.大.但是,假设我希望NSDataDetector假装当前日期是2014年1月14日.这样,当我解析“明天下午4点”时,我将得到2014-01-15T16:00.如果我在设备上更改系统时间,我会得到我想要的.但是,有没有办法以编程方式指定它?

  4. ios – 如何自动生成日期属性为Date而不是NSDate的NSManagedObject子类?

    我目前正在将我的项目更新为Swift3,并且我将所有的NSDate方法和扩展都移动到Date以便在应用程序中保持标准.问题是我使用Xcode自动生成我的NSManagedobject子类,它生成日期属性为NSDate而不是Date.有没有办法用日期属性作为日期生成它?

  5. ios – 如何减去日期组件?

    今天是星期五,根据NSCalendar,这是6.我可以通过使用以下内容得到这个我怎么得到上周六的工作日组件,应该是7?

  6. iOS – 友好的NSDate格式

    我需要在我的应用程序中显示帖子的日期给用户,现在我用这种格式:“5月25日星期五”.如何格式化NSDate以阅读“2小时前”的内容?使其更加用户友好.解决方法NSDateFormatter不能做这样的事情;你将需要建立自己的规则.我想像:所以这是打印’x分钟前’或’x小时前’从日期起24小时,通常是一天.

  7. ios – NSDate得到上周,上个月的问题

    我需要从当前日期开始获得上一个礼拜.所以我找到了可以重新计算当前日期添加间隔的解决方案这个参数:[[NSDatedate]dateByAddingTimeInterval:-604800.0](前一周)[[NSDatedate]dateByAddingTimeInterval:-2629743.83](取得上个月)正如我想,为了让周,这种方法运行良好,没有任何问题,因为每周有七天,间隔没有改变.但

  8. ios – Swift 3 – 比较两个日期时使用&lt;运算符

    当比较两个日期时,我可以比较使用>但不是

  9. ios – 如何通过在CloudKit中的creationDate进行查询?

    我想从CloudKit获取最后十分钟的公共/私人条目.我尝试了一些这样的效果,但失败了:但这会让我得到数据,但是我不知道我是查询一切,还是只是某种上限:我想能够查询一定的时间.这样做可能没有在客户端做创建排序逻辑吗?

  10. ios – NSDate比较,计算特定(本地)时区的“中间夜数”

    我在这里错过了什么吗?

随机推荐

  1. js中‘!.’是什么意思

  2. Vue如何指定不编译的文件夹和favicon.ico

    这篇文章主要介绍了Vue如何指定不编译的文件夹和favicon.ico,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  3. 基于JavaScript编写一个图片转PDF转换器

    本文为大家介绍了一个简单的 JavaScript 项目,可以将图片转换为 PDF 文件。你可以从本地选择任何一张图片,只需点击一下即可将其转换为 PDF 文件,感兴趣的可以动手尝试一下

  4. jquery点赞功能实现代码 点个赞吧!

    点赞功能很多地方都会出现,如何实现爱心点赞功能,这篇文章主要为大家详细介绍了jquery点赞功能实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  5. AngularJs上传前预览图片的实例代码

    使用AngularJs进行开发,在项目中,经常会遇到上传图片后,需在一旁预览图片内容,怎么实现这样的功能呢?今天小编给大家分享AugularJs上传前预览图片的实现代码,需要的朋友参考下吧

  6. JavaScript面向对象编程入门教程

    这篇文章主要介绍了JavaScript面向对象编程的相关概念,例如类、对象、属性、方法等面向对象的术语,并以实例讲解各种术语的使用,非常好的一篇面向对象入门教程,其它语言也可以参考哦

  7. jQuery中的通配符选择器使用总结

    通配符在控制input标签时相当好用,这里简单进行了jQuery中的通配符选择器使用总结,需要的朋友可以参考下

  8. javascript 动态调整图片尺寸实现代码

    在自己的网站上更新文章时一个比较常见的问题是:文章插图太宽,使整个网页都变形了。如果对每个插图都先进行缩放再插入的话,太麻烦了。

  9. jquery ajaxfileupload异步上传插件

    这篇文章主要为大家详细介绍了jquery ajaxfileupload异步上传插件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. React学习之受控组件与数据共享实例分析

    这篇文章主要介绍了React学习之受控组件与数据共享,结合实例形式分析了React受控组件与组件间数据共享相关原理与使用技巧,需要的朋友可以参考下

返回
顶部