- 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
- Creo Parametric 8.0中文版基礎入門一本通
- WordPress 2.7 Cookbook
- Microsoft Forefront UAG 2010 Administrator's Handbook
- HTML5 Multimedia Development Cookbook
- Excel 2010 商務數據分析與處理(第2版)
- Photoshop CS6完美創意設計:不一樣的圖像藝術處理
- 平面設計制作標準教程(微課版 第2版)
- 中文版Photoshop CS6從新手到高手(超值版)
- Photoshop CS6中文版基礎與實例教程(第6版)
- 企業微信公眾平臺開發實戰:再小的個體也有自己的品牌
- Oracle Application Express Forms Converter
- Web Host Manager Administration Guide
- AI繪畫精講:Stable Diffusion從入門到精通
- 和秋葉一起學:秒懂Photoshop后期修圖
- 中文版CATIA V5 技術大全