I have an NSDate that I get from a UIDatepicker.
IBOutlet UIDatePicker *dueDate;
NSDate *selectedDate = [dueDate date];
How do I retrieve the month from dueDate in the form of an int? (1 for Jan, 2 for Feb, etc.)
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSMonthCalendarUnit fromDate:[dueDate date]];
NSInteger month = [components month];
use -[NSCalendar components:fromDate:] for instance the example here:
http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/Reference/NSCalendar.html
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] initWithDateFormat:#"%m" allowNaturalLanguage:NO] autorelease];
int month = [[dateFormat stringFromDate:dueDate] intValue];
Related
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 have a date in UILabel(like 'Dec 14, 2010). This date i want to set as minimum date of my UIDatepicker. How can i do this please reply?
Thanks
Nishant
Convert your UILabel text in to date format using NSDateFormatter and put that value replacing "Date" in following example:
UIDatepicker *datePicker;
NSDate *Date=[NSDate date];
datePicker.minimumDate=Date;
You can use NSDateFormatter and use setDateFormat method for setting Date format.
[formatter setDateFormat:#"MMM dd,yyyy"];
NSDate *date = [formatter dateFromString:lblDate.text];
datePicker.minimumDate=date;
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
[comps setYear:0];
NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-5];
NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[datePicker setMaximumDate:maxDate];
[datePicker setMinimumDate:minDate];
it showing five year date.......
See the minimum time from the current date to one month:
NSDate* currentTime = [NSDate date];
[datePicker setMinimumDate:[currentTime dateByAddingTimeInterval:43200]];//min time +12:00 for the current date
[datePicker setMaximumDate:[currentTime dateByAddingTimeInterval:2592000]]; // max day (+ 30 )
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.
A seemingly simple question...how can I return a list of days for any specified month?
NSDate *today = [NSDate date]; //Get a date object for today's date
NSCalendar *c = [NSCalendar currentCalendar];
NSRange days = [c rangeOfUnit:NSDayCalendarUnit
inUnit:NSMonthCalendarUnit
forDate:today];
I basically want to use that, but replace today with say, the month of January, so I can return all of those days
Carl's answer works on Mac. The following works on Mac or iPhone (no dateWithNaturalLanguageString: available there).
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
// Set your year and month here
[components setYear:2015];
[components setMonth:1];
NSDate *date = [calendar dateFromComponents:components];
NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
NSLog(#"%d", (int)range.length);
You can make your date with pretty much any string:
NSDate *date = [NSDate dateWithNaturalLanguageString:#"January"];
Then the rest of your code will work as-is to give you back the NSRange for the number of days in January.
Can someone help me out with something.
I'm trying to get the current part of the day, am or pm. Is there an easy way of accomplishing this?
Thanks.
Sure, use NSDateFormatter:
NSDate *now = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat: #"a"];
NSLog ([formatter stringFromDate: now]);
[formatter release];
Outputs:
2009-08-06 14:20:35.538 yourApp [4971:20b] PM
Maybe there is an easier way .. but this works.
NSDate *now=[NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:NSHourCalendarUnit fromDate:now];
int hour = [components hour];
if ( hour > 12 ){
NSLog(#"PM %d",hour);
} else {
NSLog(#"AM");
}