objective-c: converting date string into day of the week + month name - iphone

A beginner's problem, but I was wondering if anyone could help me with this:
I need to set four strings according to a string which contains a certain date (e.g. #"Apr 7, 2011"):
a string which will take the day of the week (abbreviated: Mon, Tue, Wed, Thu, Fri, Sat, Sun): e.g. #"Thu"
a string which will take the day, e.g. #"7"
a string which will take the month, e.g. #"April"
and a string which will take the year, e.g. #"2011"
So far, I found this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:118800];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setLocale:usLocale];
NSLog(#"Date for locale %#: %#",
[[dateFormatter locale] localeIdentifier], [dateFormatter stringFromDate:date]);
// Output:
// Date for locale en_US: Jan 2, 2001
So this would give me a certain formatted date. I was wondering, however, how I can access certain parts of this date. There is the - (NSArray *)weekdaySymbols but I have no idea how to work this one and the documentation is quite frugal.
Any hints from calendar experts would be very welcome.
EDIT:
I guess this is part of the solution:
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"];
NSString *gbFormatString = [NSDateFormatter dateFormatFromTemplate:#"EdMMM" options:0 locale:gbLocale];
NSLog(#"gbFormatterString: %#", gbFormatString);
// Output: gbFormatterString: EEE d MMM, e.g. Thu 7 Apr

n.evermind,
You're going to need something like this:
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"MMM dd, yyy"];
date = [formatter dateFromString:#"Apr 7, 2011"];
NSLog(#"%#", [formatter stringFromDate:date]);
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit;
NSDateComponents *components = [calendar components:units fromDate:date];
NSInteger year = [components year];
NSInteger month=[components month]; // if necessary
NSInteger day = [components day];
NSInteger weekday = [components weekday]; // if necessary
NSDateFormatter *weekDay = [[[NSDateFormatter alloc] init] autorelease];
[weekDay setDateFormat:#"EEE"];
NSDateFormatter *calMonth = [[[NSDateFormatter alloc] init] autorelease];
[calMonth setDateFormat:#"MMMM"];
NSLog(#"%# %i %# %i", [weekDay stringFromDate:date], day, [calMonth stringFromDate:date], year );
output
2011-04-07 12:49:23.519 test[7296:207] Apr 07, 2011
2011-04-07 12:49:23.521 test[7296:207] Thu 7 April 2011
Cheers, Jordan

You should take a Look on NSDateFormatter

#n.evermind
You should actually look at NSCalendar and specifically the - components:fromDate method which gives you the NSDateComponents object which has all the peices you want.

Related

Get specific date - Objective c iphone

I want to get the date that will be in 45 days from today in objective C - iphone.
I'm new in objective C :)
I know that i can do somthing like:
NSString *dateStr = #"Tue, 16 April 2013 13:00:00 +0000";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EE, d LLLL yyyy HH:mm:ss Z"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat release];
But, i do not want a static allocate... it should be dynamic.
Every day i need to get in *date variable the date of 45 days from today.
Try this way:
NSDate *today=[NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *components=[[NSDateComponents alloc] init];
components.day=45;
NSDate *targetDate =[calendar dateByAddingComponents:components toDate:today options: 0];

how to Parse date String

i use this code to get informations from strings with this format "01-05-2011"
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"dd'-'MM'-'yyyy"];
// Your date represented as a NSDate
NSDate *dateDepart = [formatter dateFromString:daparatureFly.date];
NSDateComponents *comps = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
fromDate:dateDepart];
Then when i take the month like this [comps day],[comps month] , i have 1 and 5 , i want to have 01, 05 . Any one can help me ? thanx
Those components are NSSintegers, they are not formatted. When you use it in string you can do as follows:
NSString *day = [NSString stringWithFormat:#"%02d", [comps day]];
Something akin to...
//UNTESTED
[NSString stringWithFormat:#"%02d", [comps day]];

Compare current time with fixed time 05:00:00 PM

How will I compare current time [NSDate date] with fixed time 05:00:00 PM.
That 05:00 PM is already passed or not. I just need BOOL check for this.
- (BOOL)past5pm
{
NSCalendar *gregorianCalender = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *components = [gregorianCalender components:NSHourCalendarUnit fromDate:[NSDate date]];
if([components hour] >= 17) // NSDateComponents uses the 24 hours format in which 17 is 5pm
return YES;
return NO;
}
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"HH.mm"];
NSDate *currentDate = [NSDate date];
NSString *curDate = [dateFormatter stringFromDate:currentDate];
if ([curDate doubleValue] >= 17.00)
{
//set your bool
}
Try this
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"hh:mm:ss a"];
NSDate* date = [NSDate date];
//Get the string date
NSString* str = [dateFormatter stringFromDate:date];
NSDate *firstDate = [dateFormatter dateFromString:#"05:00:00 PM"];
NSDate *secondDate = [dateFormatter dateFromString:str];
NSTimeInterval timeDifference = [secondDate timeIntervalSinceDate:firstDate];
NSLog(#"Time Diff - %f",timeDifference);
You can probably use plain C style code and get the difference as an integer and then decide what you need to return from the comparison function depending on wether the difference is positive or negative. You can compare minutes this way as well. Dont forget to import time.h.
time_t now = time(NULL);
struct tm oldCTime;
localtime_r(&now, &oldCTime);
int hours = oldCTime.tm_hour;
int diff = 17-hours;
NSLog(#"Time difference is: %d.", diff);

why does this code display the date being SAT where with NSDateComponent setWeekday I used 1 for SUN?

Why does this code display the date being SAT where with NSDateComponent & setWeekday I used 1 for SUN?
// Set Day
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setWeekday:1];
[comps setHour:13];
[comps setMinute:30];
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *tempDate = [gregorian dateFromComponents:comps];
NSDateFormatter *format = [[[NSDateFormatter alloc] init] autorelease];
[format setDateFormat:#"EEE HH:mm"];
NSString *s = [format stringFromDate:tempDate];
NSLog(#"Finishing : %#", s);
// Gave => 2011-04-05 09:50:28.467 test1[39840:207] Finishing : Sat 13:30
Converting an NSDateComponents with only the weekday, hour, and minute set apparently gives you a date in the year 1. As this is long before the Julian/Gregorian calendar transition, you will end up with odd results in some places where some methods interpret the date in the proleptic Gregorian calendar while others interpret it in the Julian calendar.
When I try your code with a more detailed date format, I get Sat 0001-01-01 13:30:00 -045602. Try it after using setYear: on the components and you'll get sensible results.

How to find days between dates stored in 2 strings

I have two strings both are in following date format (2011-03-22).
i have to compare them and find number of days between them.
Can anyone tell me how to do this..
Please also tell me the correct method to convert them back to NSDate.
Might be a couple of errors as i just typed this, but it speaks for itself and you should get the idea.
NSString *dateStringA = #"2011-03-22";
NSString *dateStringB = #"2011-03-27";
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
df.dateFormat = #"yyyy-MM-dd";
NSDate *dateA = [df dateFromString:dateStringA];
NSDate *dateB = [df dateFromString:dateStringB];
NSDateComponents *comps = [[NSCalendar currentCalendar] components:NSDayCalendarUnit fromDate:dateA toDate:dateB options:0];
int daysBetweenDates = comps.day; //This is your days between the 2 dates
NSDate *intervalDate = [[NSCalendar currentCalendar] dateFromComponents:comps]; //This date object represents the duration between them
 For string to date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyy-MM-dd HH:mm:ss ZZZ"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:<NSString containing date>];
for date to string
NSString *date = [[NSDate date] description];