Code older, for reference
Nscalendar is used to deal with time-related issues. such as the comparison of time before and after the calculation date of the Zhou Bie and so on.
1. The following methods are available for creating or initializing
+ (ID) Currentcalendar;
Get the current user's logical calendar (logical calendars)
+ (ID) Autoupdatingcurrentcalendar;
Get the current user's logical calendar (logical calendar), ...
-(ID) Initwithcalendaridentifier: (NSString *) identifier;
Initialize to various calendars. The range of identifier can be:
Nsgregoriancalendar Solar Calendar
Nsbuddhistcalendar Buddhist Calendar
Nschinesecalendar China Calendar
Nshebrewcalendar Hebrew Calendar
Nsislamiccalendar Islamic Calendar
Nsislamiccivilcalendar Islamic Civil calendar
Nsjapanesecalendar Japan Calendar
2. If necessary before use, the following settings can be done first
-(void) SetLocale: (Nslocale *) locale;
Set Area
-(void) Settimezone: (Nstimezone *) TZ;
Setting the time zone
-(void) Setfirstweekday: (nsuinteger) value;
Set the first day of the week from the beginning of the week, for example:
. If you want to set the value to be passed in from Sunday onwards, 1
. If you want to set the value to be passed in from Monday onwards, 2
. And so on
For more information, see Firstweekday Properties
-(void) Setminimumdaysinfirstweek: (nsuinteger) value;
Set the minimum number of days that must be included as the first week (yearly and monthly), such as:
. If you want to set the first week to include at least 7 days, value is passed in 7
For more information, see Minimumdaysinfirstweek Properties
3. Retrieve the settings information in the following ways
-(NSString *) calendaridentifier
Returns the calendar identifier (identifier). Valid calendar identifiers include the following:
Nsgregoriancalendar
Nsbuddhistcalendar
Nschinesecalendar
Nshebrewcalendar
Nsislamiccalendar
Nsislamiccivilcalendar
Nsjapanesecalendar
-(Nslocale *) locale;
Returns the region information specified by the calendar. It will affect???
-(Nstimezone *) TimeZone;
Returns the time zone information specified by the calendar. It will affect???
-(Nsuinteger) Firstweekday;
Returns the first day of the week specified by the calendar starting from the beginning of the week. The default is Sunday, which is Firstweekday = 1
When method [Nscalendar ordinalityOfUnit:inUnit:fromDate:]
When the Ordinalityofunit parameter for the Nsweekdaycalendarunit,inunit parameter is Nsweekcalendarunit,
The Firstweekday property affects its return value. Specify the following:
. When Firstweekday is specified as Sunday (that is, = 1), the value it returns corresponds to the number of days of the week. Like what:
FromDate The parameter passed in is Sunday, the function returns 1
FromDate The parameter passed in is Monday, the function returns 2
. When Firstweekday is specified as a different value (that is, <> 1), assuming that Firstweekday is specified as Monday (that is, = 2), then:
FromDate The parameter passed in is Monday, the function returns 1
FromDate The parameter passed in is Tuesday, the function returns 2
FromDate The parameter passed in is Sunday, the function returns 7
-(Nsuinteger) Minimumdaysinfirstweek;
Returns the minimum number of days that must be included in the first week specified by the calendar. The default is 1, which is Minimumdaysinfirstweek = 1.
When method [Nscalendar ordinalityOfUnit:inUnit:fromDate:]
When the Ordinalityofunit parameter for the Nsweekcalendarunit,inunit parameter is Nsyearcalendarunit,
The Minimumdaysinfirstweek property affects its return value. Specify the following:
January 2005
Day 123456
--------------------------------
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
The first week of January 2005 includes number 1th.
A. If the Minimumdaysinfirstweek setting = 1
Then fromdate passed in January 1, the method returns 1 ==> the number of days specified by Minimumdaysinfirstweek (minimum 1 days), so the method is classified as the 1th week of 2005 years
Then fromdate incoming January 2-8, the method returns 2
Then fromdate incoming January 9-15, the method returns 3
......
B. If the Minimumdaysinfirstweek is set to > 1, for example 2
FromDate incoming January 1, the method returns ==> less than 2 days, so the method will be classified as the 53rd week of 2004 years
Then fromdate incoming January 2-8, the method returns 1
Then fromdate incoming January 9-15, the method returns 2
......
January 2008
Day 123456
---------------------------------
1 2 3) 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29) 30 31
The first week of January 2005 includes a total of 5 days in number 1-5.
A. If Minimumdaysinfirstweek is set to <= 5 o'clock
Then fromdate incoming January 1-5, the method returns 1 ==> satisfies the Minimumdaysinfirstweek specified number of days, so the method is classified as the 1th week of 2008 years
Then fromdate incoming January 6-12, the method returns 2
Then fromdate incoming January 13-19, the method returns 3
......
B. If the Minimumdaysinfirstweek is set to > 5, for example 6
Then fromdate incoming January 1-5, the method returned to ==> when the week is less than 6 days, so the method will be classified as the 53rd week of 2007 years
Then fromdate incoming January 2-8, the method returns 1
Then fromdate incoming January 9-15, the method returns 2
......
When method [Nscalendar ordinalityOfUnit:inUnit:fromDate:]
When the Ordinalityofunit parameter for the Nsweekcalendarunit,inunit parameter is Nsmonthcalendarunit,
The Minimumdaysinfirstweek property affects its return value. In April 2008, for example, the following are specified:
April 2008
Day 123456
---------------------------------
1 2 3) 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
The first week of April 2008 includes numbers 1, 2, 3, 4, 5th.
1. If the Minimumdaysinfirstweek is set to a number less than or equal to 5
Then fromdate incoming April 1-5, the method returns 1
Then fromdate incoming April 6-12, the method returns 2
Then fromdate incoming April 13-19, the method returns 3
....
2. If the Minimumdaysinfirstweek is set to a number greater than 5
Then fromdate incoming number 1-5, the method returns 0
Then fromdate incoming number 6-12, the method returns 1
Then fromdate incoming number 13-19, the method returns 2
....
Description of other methods
-(Nsrange) Maximumrangeofunit: (nscalendarunit) unit;
Returns the maximum range of cells. As for Gregorian, there are:
Nseracalendarunit = 0-2
Nsyearcalendarunit = 1-10000
Nsmonthcalendarunit = 1-12
Nsdaycalendarunit = 1-31
Nshourcalendarunit = 0-24
Nsminutecalendarunit = 0-60
Nssecondcalendarunit = 0-60
Nsweekcalendarunit = 1-53
Nsweekdaycalendarunit = 1-7
Nsweekdayordinalcalendarunit =? (2147483647-2147483647)
(Nsnotfound-nsnotfound)
-(Nsrange) Minimumrangeofunit: (nscalendarunit) unit;
Returns the minimum range of cells. As for Gregorian, there are:
Nseracalendarunit = 0-2
Nsyearcalendarunit = 1-10000
Nsmonthcalendarunit = 1-12
Nsdaycalendarunit = 1-28 * * *
Nshourcalendarunit = 0-24
Nsminutecalendarunit = 0-60
Nssecondcalendarunit = 0-60
Nsweekcalendarunit = 1-52 * * *
Nsweekdaycalendarunit = 1-7
Nsweekdayordinalcalendarunit =? (2147483647-2147483647)
(Nsnotfound-nsnotfound)
-(Nsuinteger) Ordinalityofunit: (nscalendarunit) smaller inunit: (nscalendarunit) larger fordate: (nsdate *) Date
Returns the order of a specific time (date) of its corresponding small time cell (smaller) in a large time cell (larger), such as:
. To get 2008/11/12 in the first week of the month you can call this method:
[Calendar Ordinalityofunit:nsweekordinalcalendarunit inunit:nsweekcalendarunit fordate:somedate];
Note: somedate was stored in 2008/11/12
. To get 1:50 minutes in the first hour of the day, you can call this method:
[Calendar Ordinalityofunit:nshourcalendarunit inunit:nsdaycalendarunit fordate:sometime];
Note: The sometime is stored 1:50
. Please refer to Firstweekday and Minimumdaysinfirstweek for other instructions.
-(Nsrange) Rangeofunit: (nscalendarunit) smaller inunit: (nscalendarunit) larger fordate: (nsdate *) Date
Returns the range of a specific time (date) of its corresponding small time cell (smaller) in a large time cell (larger), such as:
. To get 2008/11/12 in the date range of the month in which you can call this method:
[Calendar Ordinalityofunit:nsdaycalendarunit inunit:nsmonthcalendarunit fordate:fdate];
Then 1-31 is returned. Note: fdate was stored in 2008/11/12
. To get 2008/02/20 in the date range of the month in which you can call this method:
[Calendar Ordinalityofunit:nsdaycalendarunit inunit:nsmonthcalendarunit fordate:fdate];
Then 1-29 is returned. Note: fdate was stored in 2008/11/12
-(BOOL) Rangeofunit: (nscalendarunit) Unit StartDate: (nsdate *) sdate interval: (Nstimeinterval *) unitsecs fordate: ( NSDate *) Date;
Used to return the start time (sdate) of the Calendar unit Unit (parameter) where the date dates (parameters) are located. Where the parameter unit specifies the calendar cell, the parameter sdate is used to return the first day of the calendar cell, the parameter unitsecs is used to return the length of the calendar cell (in seconds), and the parameter date specifies a specific date.
If both StartDate and interval are computable, then the function returns Yes otherwise returns No. For example:
. To calculate the first day of the week on which the 2008/01/08 day is located:
...
NSDate *dateout = [NSDate Date];
NSDate **sdate = &dateOut;
Nsdatecomponents *DC = [[Nsdatecomponents alloc] init];
[DC setyear:2008];
[DC Setmonth:1];
[DC Setday:8];
NSDate *date = [Calendar DATEFROMCOMPONENTS:DC];
Nstimeinterval intout;
Nstimeinterval *intervalout = &intOut;
if ([Calendar Rangeofunit:nsmonthcalendarunit startdate:sdate interval:interval0 fordate:date]) {
NSLog (@ "%@", [*sdate description]);
NSLog (@ "%f", *intervalout);
}
else{
NSLog (@ "Can not be calculated!");
}
...
On the console window you can see:
2008-01-06 00:00:00 +0800
604800.000000
Note: 2008-01-06 is the first day of the week in 2008/01/08, and 604800 is the number of seconds in 7 days of the week.
. To calculate the first day of the week on which the 2008/01/05 day is located, you can see on the console window:
2007-12-30 00:00:00 +0800
604800.000000
. To calculate the first day of the month on which the 2008/02/06 day is located, you can see on the console window:
2007-02-01 00:00:00 +0800
2505600.000000
Note: 2505600 is the number of seconds in February for 29 days.
The processing of dates and times Nscalendar