I am attempting to grab a date using a specified timezone name, for example "America/New_York". For some reason, when I choose my correct timezone "America/Chicago", it shows the correct time (e.g. 9:41PM). However, when I choose New York, it should show an hour ahead (expecting 10:41PM), but I get an hour behind my current time (what I get: 8:41PM).
- (NSDate*)dateFromTimezone:(NSTimeZone*)timezone
{
timezone = ( timezone == nil ? [NSTimeZone timeZoneWithName:CUSTOMTIMEZONE] : timezone );
NSDate * date = [NSDate date];
NSCalendarUnit units = NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:timezone];
[gregorian components:units fromDate:date];
NSDateComponents* dateComponents = [[NSCalendar currentCalendar] components:units fromDate:date];
[dateComponents setTimeZone:timezone];
[dateComponents setCalendar:gregorian];
return [gregorian dateFromComponents:dateComponents];
}
Related
How can I create an NSDate with today's date and an hour, minutes, and seconds of 5, 0, and 0 respectively? I.e. the date will be 07/02/2010 05:00:00
//Gives us the current date
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[NSDate date]];
[components setHour:5];
NSDate * date = [gregorian dateFromComponents:components];
[gregorian release];
Something along those lines should do the trick.
I am working on a Calender app and it's working fine when i navigate to next month as i am able to get the last day of the month and from where i am able to get the first day of coming month .but when i navigate to previous month i m not able to get the last month's first day.Is there any way to get first day of last month.
The following works fine for me
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *components = [calendar components:(NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:currentDate];
components.month = components.month - 1;
components.day = 1;
NSDate *newDate = [calendar dateFromComponents:components];
This also works if your current month is january. NSDateComponents will automatically decrement the year and set the month to december.
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear
fromDate:[NSDate date]];
components.day = 1;
components.month = components.month - 1;
NSDate *lastmonthDay1 = [[NSCalendar currentCalendar] dateFromComponents: components];
NSLog(#"First day of last month====: %#", [lastmonthDay1 descriptionWithLocale:[NSLocale currentLocale]]);
You can do something like below....
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDateComponents *comp = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate date]];
[comp setDay:1];
[comp setMonth:CurrentMonth];//previous month
[comp setYear:currentYear];
NSDate *firstDayOfMonthDate = [gregorian dateFromComponents:comp];
NSCalendar *cal1=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps1 = [cal1 components:NSWeekdayCalendarUnit fromDate:firstDayOfMonthDate];
// 1 for mon 8 for sun
int startWithDay=[comps1 weekday]==1?8:[comps1 weekday];
//1 for monday
// 2 for tuesday and so on....
Let me know it is working or not!!!
Happy Coding!!!!
Is there any easier way to assign only hour and minute from one NSDate A to NSDate B, but keep B's Year Month and Day?
I don't know If it's an easier way, but it's the correct way:
NSDate *dateA = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSUInteger timeComps = (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSTimeZoneCalendarUnit);
NSDateComponents *comps = [gregorian components:timeComps fromDate:dateA];
[components setHour: 3];
[components setMinute: 42];
NSDate *newDateB = [gregorian dateFromComponents: components];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit *dateComponent = (NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit);
NSDateComponents *components1 = [calendar components:dateComponent fromDate:dateA];
NSInteger day = components1.day;
NSInteger month = components1.month;
NSInteger year = components1.year;
NSDateComponents *components2 = [calendar components:dateComponent fromDate:dateB];
NSInteger hour = components2.hour;
NSInteger minute = components2.minute;
NSInteger second = components2.second;
NSDateComponents *requiredComponent = [[NSDateComponents alloc] init];
requiredComponent.day = day;
requiredComponent.month = month;
requiredComponent.year = year;
requiredComponent.hour = hour;
requiredComponent.minute = minute;
requiredComponent.second = second;
NSDate *date = [calendar dateFromComponents:requiredComponent];
i have an NSDate object from an old date which i am getting from my DB. Now i need to set my UIDatePicker date to current date but with hour and minute value from my stored NSDate object which i have got from my DB.
So how to extract hour and minute from old date and set it to current UIDatePicker NSdate.
Thanks
Since a NSDate can't be changed after it's initialization, you have to recreate it:
To get the hour and minute component of the NSDate use NSCalendar:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:yourOriginalDateHere];
NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];
Then you can create a new NSDate (also using NSCalendar):
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate date]]; //[NSDate date] is the current date
[dateComponents setHour:hour];
[dateComponents setMinute:minute];
[dateComponents setSecond:second];
NSDate *newDate = [gregorian dateFromComponents:dateComponents];
NSCalendar *cal = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *dbDateComps = [cal components:(kCFCalendarUnitHour | kCFCalendarUnitMinute) fromDate:YOUR_DATE_FRM_DB];
NSDateComponents *currentComps = [cal components:(kCFCalendarUnitYear | kCFCalendarUnitMonth | kCFCalendarUnitDay) fromDate:[NSDate date]];
[currentComps setHour:[dbDateComps hour]];
[currentComps setMinute:[dbDateComps minute]];
NSDate *resultDate = [currentComps date];
NSDate *today = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setLocale:[NSLocale currentLocale]];
[calendar setTimeZone:[NSTimeZone timeZoneWithName:#"US/Central"]];
NSDateComponents *nowComponents = [calendar components:NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today];
[nowComponents setHour:hour];
[nowComponents setMinute:0];
[nowComponents setSecond:0];
NSLog(#"%#", [calendar dateFromComponents:nowComponents]);
logs 2012-01-01 06:00:00 +0000. The time is correct based on the timezone setting, but today is July 20th... What am I doing wrong?
You need to extract the year, month, and day components, and construct a new NSDate, like this:
NSDate *today = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setLocale:[NSLocale currentLocale]];
[calendar setTimeZone:[NSTimeZone timeZoneWithName:#"US/Central"]];
NSDateComponents *nowComponents = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today];
today = [calendar dateFromComponents:nowComponents];
You do not need to get the time components and then set them back to zero: you can keep them at their defaults by not asking to extract them in the first place.