Im using an NSCalendar to display the week day, I have no problem displaying the weekday in an integer, but i am confused as to how i might get it to tell me the weekday, i.e. "Thursday." I read that %A, and %a might do the trick but I do not know how to implement them. My code is below.
NSDate* now = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents =[gregorian components:NSWeekdayCalendarUnit fromDate:now];
NSInteger weekday = [weekdayComponents weekday];
myLabel.text = [NSString stringWithFormat:#"%02d", weekday];
I use myLabel to display the string on the Iphone, but if there is a better way please let me know.
Thanks for the help.
James
To display day of week you actually need to use NSDateFormatter class, set 'EEEE' format to get full day name (like Thursday) or 'EEE' - for shortened (like Thu). Very sample code:
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:#"EEEE"];
NSLog([f stringFromDate:[NSDate date]]);
Related
I am working on a project where i need to set an alarm on 7 days before particular date the user has selected from date picker.
I used the following code to get the date from user
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateStyle:NSDateFormatterFullStyle];
NSString *dateStr = [NSString stringWithFormat:#"%#",[df stringFromDate:datePickerObj.date]];
[df release];
Can any one please tell me how to get the date of 7 days previous to the selected date.
Thanks in advance
Use dateByAddingComponents:toDate:options:, like this:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = -7;
NSDate *earlierDate = [calendar dateByAddingComponents:components
toDate:datePickerObj.date options:0];
Is there a way to subtract the current date by 5. say if today is 2008-12-9 i need to get the date 5 days back. If we output this, the date should display as 2008-12-4.
How can i code this programatically? or a tutorial that would help
Always use NSCalendar and NSDateComponents for date calculations. This will take into account oddities like leap years with 29 days in February, leap seconds and daylight saving changes.
NSDate *date = [NSDate date]; // Using current date here
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = - 5; // Find date for 5 days ago
NSDate *newDate = [calendar dateByAddingComponents:components toDate:date options:0];
Use NSDateComponents
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *offsetComponents = [[[NSDateComponents alloc] init] autorelease];
[offsetComponents setDays:-5];
NSDate *fiveDaysAgo = [gregorian dateByAddingComponents:offsetComponents toDate:today options:0];
to convert it to a string with the preferred format, use NSDateFormatter
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString *formattedDateString = [dateFormatter stringFromDate:fiveDaysAgo];
This is the brute-force way:
Sustract 5 from DAY.
If DAY < 0, add number of days of the previous month and sustract 1 from MONTH.
If MONTH < 0, add number of month of a year and sustract 1 from YEAR.
The advantage of the brute-force approach is that it will work with every language.
#define SOME_HOUR -24*5
NSDate *today = [NSDate date];
NSDate *someDay = [NSDate dateWithTimeInterval:60*60*SOME_HOUR sinceDate:today];
I want to find a date by providing a weekday. For instance, I want to find the date of last Sunday and this Sunday. How to do it in iOS?
In OSX, NSDate provides a method of dateWithNaturalLanguageString, but iOS does not provide a similar method. Please help me.
Thanks.
you can calculate using NSDateComponents..
This is Sample. It helps you.
NSDate *date = [NSDate date];
NSString *dateString = [NSDateFormatter localizedStringFromDate:date
dateStyle:NSDateFormatterLongStyle
timeStyle:NSDateFormatterNoStyle];
NSString *timeString = [NSDateFormatter localizedStringFromDate:date
dateStyle:NSDateFormatterNoStyle
timeStyle:NSDateFormatterShortStyle];
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
int weekday = [[currentCalendar components:NSWeekdayCalendarUnit fromDate:date] weekday];
NSString *weekdayString = [[[NSDateFormatter alloc] shortWeekdaySymbols] objectAtIndex:weekday-1];
Can any one tell that how to find the date for the 3rd day from the current date iphone/ipad.
Thanks in advance
You can use this:
NSDate *today = [[NSDate alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:3];
NSDate *threeDaysFromToday = [gregorian dateByAddingComponents:offsetComponents toDate:today options:0];
Slightly modified example from Apple's own documentation on NSDate. Check the link out for further info and more examples.
Here is Sample Code
NSDate * currentDate=[NSDate date];
NSTimeInterval interval=[currentDate timeIntervalSince1970];
NSTimeInterval intervalForThirdDate=interval+86400*3;
NSDate *nextDate=[[NSDate alloc]initWithTimeIntervalSince1970:intervalForThirdDate];
NSDate *today = [NSDate date];
NSTimeInterval threeDays = 86400*3;
NSDate *threeDaysFromToday = [NSDate dateWithTimeInterval:threeDays sinceDate:today];
choose one:
NSDate *futureDate;
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setDay:3];
futureDate = [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:[NSDate date] options:0];
NSLog(#"%# - %#", [NSDate date], futureDate);
NSDate *futureDate = [[NSDate date] dateByAddingTimeInterval:3 * (24*60*60)];
[NSDate dateWithTimeIntervalSinceNow:86400*3]
But as someone mentioned above, DST might make this too inaccurate. Depends on how important that is.
Something like:
// get the gregorian calendar ready to go; use the getter for the current system
// calendar if you're happy to deal with other calendars too
NSCalendar *gregorianCalendar = [[NSCalendar alloc]
initWithCalendarIdentifier: NSGregorianCalendar];
// get an NSDate object that is three days in the future
NSDate *dateInFuture = [NSDate dateWithTimeIntervalSinceNow:3*24*60*60];
// grab the date components for the bits we want to print in this example
NSDateComponents *componentsOfDateInFuture =
[gregorianCalendar
components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
fromDate:dateInFuture];
// and print
NSLog(#"in three days it'll be %04d/%02d/%02d",
componentsOfDateInFuture.year,
componentsOfDateInFuture.month,
componentsOfDateInFuture.day);
// clean up
[gregorianCalendar release];
EDIT: Pablo Santa Cruz's answer is better for ensuring you're three days in the future, given daylight savings concerns. This is how you'd decompose an NSDate to day/month/year though, for the purposes of having the date rather than simply an NSDate.
I am looking for component that can handle a spesific date.
what i am trying to do is to get Astring fron sever that represent date(for example 04-08-2012) in my iphone i want to be able to "work" with this date. such to compare it to another date , check if the date in the past or future and to print it to the app GUI
i tried work with NSDate but i didnt found how can i set a spesific date?
thanks
You can use an NSDateFormatter
Here is a sample code to parse a date from string:
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"%A, %B %d, %Y"];
NSDate* date = [formatter dateFromString:aString];
[date compare:anotherDate];
More about Date Formatter here
The date format string is composed of various elements that pull out portions of the date. %A is the full name of the day of the week, %B is the full name of the month, etc.
You need 3 classes to set the date by representation:
NSCalendar* cal = [NSCalendar currentCalendar]; // 1.
NSDateComponents *comps = [[NSDateComponents alloc] init]; // 2.
[comps setYear:2012];
[comps setMonth:4];
[comps setDay:8];
NSDate* date = [cal dateFromComponents:comps]; // 3.
[comps release];