- Ext JS 3.0 Cookbook
- Jorge Ramon
- 540字
- 2021-04-01 13:43:45
Formatting, parsing, and manipulating dates
Another area where the dynamic nature of JavaScript creates challenges is dates manipulation. This recipe covers formatting, conversion, and range checking for dates.
How to do it...
You can format, convert, and range check dates as show in the following steps:
- Add the date patterns you will use to format dates in your code:
Date.patterns = { ISO8601Long: "Y-m-d H:i:s", ISO8601Short: "Y-m-d", ShortDate: "n/j/Y", LongDate: "l, F d, Y", FullDateTime: "l, F d, Y g:i:s A", MonthDay: "F d", ShortTime: "g:i A", LongTime: "g:i:s A", SortableDateTime: "Y-m-d\\TH:i:s", UniversalSortableDateTime: "Y-m-d H:i:sO", YearMonth: "F, Y" };
- Create a sample
Date
object: - Format the date using the patterns:
var ISO8601Long = now.format(Date.patterns.ISO8601Long); //ISO8601Long is similar to 2009-03-05 14:01:45 var ISO8601Short = now.format(Date.patterns.ISO8601Short); //ISO8601Long is similar to 2009-03-05 var ShortDate = now.format(Date.patterns.ShortDate); //ISO8601Long is similar to 3/5/2009 var LongDate = now.format(Date.patterns.LongDate); //ISO8601Long is similar to Thursday, March 05, 2009 var FullDateTime = now.format(Date.patterns.FullDateTime); //ISO8601Long is similar to Thursday, March 05, 2009 2:01:45 PM var MonthDay = now.format(Date.patterns.MonthDay); //ISO8601Long is similar to March 05 var ShortTime = now.format(Date.patterns.ShortTime); //ISO8601Long is similar to 2:01 PM var LongTime = now.format(Date.patterns.LongTime); //ISO8601Long is similar to 2:01:45 PM var SortableDateTime = now.format(Date.patterns.SortableDateTime); //ISO8601Long is similar to 2009-03-05T14:01:45 var UniversalSortableDateTime = now.format(Date.patterns.UniversalSortableDateTime); //ISO8601Long is similar to 2009-03-05 14:01:45-0500 var YearMonth = now.format(Date.patterns.YearMonth); //ISO8601Long is similar to March, 2009
- Create a variable to hold your parsed date:
var aDate = new Date();
- Convert a string to a date:
aDate = Date.parseDate("March, 2009", Date.patterns.YearMonth); //aDate = Thu Mar 5 00:00:00 EST 2009 aDate = Date.parseDate("2:01:45 PM", Date.patterns.LongTime); //aDate = Thu Mar 5 14:01:45 EST 2009 aDate = Date.parseDate("2009-03-05", Date.patterns.ISO8601Short); //aDate = Thu Mar 5 00:00:00 EST 2009
- For range checking, create range limits:
var low = Date.parseDate("July, 2008", Date.patterns.YearMonth); var high = Date.parseDate("July, 2009", Date.patterns.YearMonth);
- Check whether your date is in the range:
var now = new Date(); var inRange = now.between(low, high); // inRange is true
Ext JS enhances the JavaScript Date
object with the Ext.Date
class, which provides a number of properties and functions that simplify your work with dates.
Regarding date formats, although there isn't a central repository of format patterns in Ext JS, the Ext JS API documentation provides the ones used in the previous example. In order for these formats to become available on the Date
object, they should be copied into any script that is included after Date.js
.
Besides the functions in the examples above, Ext.Date
allows you to do things such as:
- Getting the numeric representation of the year
- Getting the number of days in the current month
- Determining the number of milliseconds between two dates
- Getting the date of the first day of the month in which a date resides
- Getting the first day of the current month
- Getting the offset from GMT of the current date
- Getting the date of the last day of the month in which a date resides
- Getting the last day of the current month
- Getting the month number for the given short/full month name
- Getting the short day name for a given day number
- Getting the short month name for a given month number
- Determining if a date is in a leap year
- TArch 8.5天正建筑軟件標準教程
- Hi!扁平化Photoshop扁平化用戶界面設計教程
- Dreamweaver基礎與實戰教程
- 3ds max & VRay產品造型設計經典
- Oracle VM Manager 2.1.2
- 好的PPT會說話:如何打造完美幻燈片
- Python數據分析實戰:從Excel輕松入門Pandas
- Drupal 6 Panels Cookbook
- SPSS 28.0統計分析從入門到精通(升級版)
- 3ds Max 2015中文版從入門到精通
- Python Testing: Beginner's Guide
- Transformer自然語言處理實戰:使用Hugging Face Transformers庫構建NLP應用
- Altium Designer 20 中文版從入門到精通
- Photoshop 2021中文版入門、精通與實戰
- AutoCAD 2018中文版完全自學手冊