I need a array with date units, for example Year,Month,Day....
I want them to be localized, for example in german it would be Jahr, Monat, Tag ...
It seems I can only get the localized string from date but not the units.
Is there a way to get those units from current calendar directly instead of translating them by hand?
As far as I know, you will need to provide your own translations for those units. You can get the names of the days and months with NSDateFormatter but not the units themselves. How the unit is translated depends on grammatical case and number, so the translation is context sensitive. Don't forget, some languages have grammatical numbers other than just singular and plural!
NSCalendar *gregorian=[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"CDT"]];
NSDateComponents *timeZoneComps=[[NSDateComponents alloc] init];
[timeZoneComps setHour:16];
//specify whatever day, month, and year is appropriate
NSDate *date=[gregorian dateFromComponents:timeZoneComps];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
NSString *monthName = [[df monthSymbols] objectAtIndex:(mymonth-1)];
[df setDateFormat:#"EEE"];
NSString *weekName = [ df stringFromDate:date];
NSLog(#"The Week Name :: %#",weekName);
[df release];
You can use above code and particularly timeZoneWithAbbreviation:#"CDT" represents the abbreviation of various timezones like CDT, GMT,UTC,CST. I think it will be helpful to you.
Related
In my application i get from a server some dates.
Some of the dates are in this format : "MMM dd, yyyy hh:mm:ss a" and some others in this : "yyyy-MM-dd HH:mm:ss Z" . I get the dates as strings from the server in one of the above formats. However i want all my dates to be in the first format. So how can i compare if the date is on the second format so i can change it? I know how to change formats , i just dont know how to compare a date with a specific format.
Thanks!
If you know the format that any given returned string is in then you could explicity set the format for each iteration of getting a date and then convert it to whatever format you need. Something like this example;
NSString *myString1 = #"01-30-2012";
NSString *myString2 = #"30-01-2012";
// Now convert the string to a date object
NSDateFormatter *myDateFormat = [[NSDateFormatter alloc] init];
[myDateFormat setDateFormat:#"dd-MM-yyyy"];
NSDate *date1 = [myDateFormat dateFromString:myString1];
[myDateFormat setDateFormat:#"MM-dd-yyyy"];
NSDate *date2 = [myDateFormat dateFromString:myString2];
[myDateFormat release];
If those are the only formats, just check something simple like if the fifth character is a dash then it's second, otherwise first.
I have a list of birthdays. From the list of b'days, I need to select all the b'days which occurs in the next 30 days.
For example : Today's date is 07/13, then I need to list all the b'days which occurs between 07/13 and 08/13.
Is there any built in method to select the dates in this manner.
Thanks in advance
You can use NSPredicate for this:
NSDate *now = [NSDate date];
NSDate *later = [now dateByAddingTimeInterval:(30*24*60*60)];
NSPredicate *pre = [NSPredicate predicateWithFormat:#"SELF >= %# AND SELF <= %#", now, later];
NSArray *upcomingBirthdays = [birthdays filteredArrayUsingPredicate:pre];
You going to have to work the logic out for yourself, here is a link to "Date and Time Programming Guide". It contains some sample code to check if "Date" falls within a Week. Might be a good place to start.
Apple's guide
I have a project that I need to get the current date/time. From that date/time I need to convert to 6 different timezone's date/time. That is no problem converting the times.
Then from each of the converted date/time I need to see if that date is between two other dates that change every week. I am guessing the times that change everyweek could be an array of dates. The current date/time in that timezone needs to find out which date/time in the array it needs to check itself against. The nearest before and the nearest after. This sounds very confusing just typing this.
Any help pointing me in the right direction would be extremely helpful.
Thankyou
NSDate *date1;
NSDate *date2;
NSDate *date3;
NSTimeInterval firstTimeInterval = [date1 timeIntervalSince1970];
NSTimeInterval secondTimeInterval = [date2 timeIntervalSince1970];
NSTimeInterval thirdTimeInterval = [date3 timeIntervalSince1970];
if (firstTimeInterval<secondTimeInterval && secondTimeInterval<thirdTimeInterval) {
// date2 > date1 and date2 < date3
}
Of course, in my case it would crash since dates have no addresses, but it's just an example... Yours would need to have actual dates in them.
To check if a date is in a specified range just get the unix timestamps and compare them directly like so:
NSDate *lowerLimit = ...
NSDate *upperLimit = ...
NSDate *myDate = ...
BOOL inRange = (lowerLimit.timeIntervalSince1970 <= myDate.timeIntervalSince1970) &&
(upperLimit.timeIntervalSince1970 >= myDate.timeIntervalSince1970);
You could also use NSDate's -compare: method, but I think it's more natural to compare the timestamps.
Is there a way (an elegant way that is) to extract a date format from a string containing a date so that it may be converted into an NSDate via NSDateFormatter?
i.e
string = #"2011-1-10";
format = [extractor extractFormat:string];// format would = yyyy-dd-mm
[formatter setDateFormatter:format];
NSDate * date = [formatter dateFromString:string];
If that is the only information you have then it is impossible. For example, "10-10-10".
To be more precise this is not working for me:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"YYYY-MM-ddTHH:mm:ssZ"];
NSDate *prevday = [formatter dateFromString:#"2011-04-07T22:00:48Z"];
prevday is returning NIL.
You need to inserts ticks in the string:
#"YYYY'-'MM'-'dd'T'HH':'mm':'ss'Z'"
According to this document;
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html%23//apple_ref/doc/uid/TP40002369-SW1
Date formatters use the following version of the standard;
http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns
so you'd use the date format 'yyyy-MM-ddTHH:mm:ss' I'm not sure about the Z, is that supposed to be the time zone? Z in the format string will give you the 3 letter time zone name.
Edited based on your edit above:
[formatter setDateFormat:#"YYYY-MM-ddTHH:mm:ssZ"];
Is why it's returning nil.
The 'Z' expects the time zone to be in the 3 character time zone thing...
Drop the Z or escape it with a quote character. Read the tr35 doc above for more info.