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.
Related
I'm trying to set the month and year of [NSDate date] but the function dateFromComponents return the wrong value, this is my code:
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian]; // Setup an NSCalendar
NSDateComponents *components = [gregorianCalendar components: NSUIntegerMax fromDate: [NSDate date]]; // Setup NSDateComponents
[components setTimeZone:[NSTimeZone defaultTimeZone]];
[components setMonth:monthInt];
[components setYear:yearInt];
// Setup the new date
NSDate *dateToCompare = [gregorianCalendar dateFromComponents: components];
yearInt is 2014 and monthInt is 12, but it reports back 12-2015 and not 2014, why is this?
Thanks!
you need to put only the bit operator you need, try this:
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian]; // Setup an NSCalendar
NSDateComponents *components = [gregorianCalendar components: NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear |NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour | NSCalendarUnitTimeZone fromDate: [NSDate date]]; // Setup NSDateComponents
[components setTimeZone:[NSTimeZone defaultTimeZone]];
[components setMonth:12];
[components setYear:2014];
// Setup the new date
NSDate *dateToCompare = [gregorianCalendar dateFromComponents: components];
NSLog(#"Ver %#",dateToCompare);
NSUIntegerMax = 18446744073709551615
In bit representation: 10000000000000000000000000000000000000000000000000000000000000000
To many switch off.
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];
}
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!!!!
I am writing an alarm app. In the app user can select days for setting alarm on that day with time. But I don't know to set UILocalNotification for specific day in a week.
How I know about repeat interval. I know that I must to set
How to calculate how many days will come Friday?
How to Get an NSDate for a Specific Day of Week and Time from an Existing NSDate - this article will help you with this problem. You can get NSDate from current Friday with:
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setLocale:[NSLocale currentLocale]];
NSDateComponents *nowComponents = [gregorian components:NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today];
[nowComponents setWeekday:6];
[nowComponents setWeek: [nowComponents week]]; // Current week
[nowComponents setHour:8]; // 8a.m.
[nowComponents setMinute:0];
[nowComponents setSecond:0];
NSDate *thisWeekFriday = [gregorian dateFromComponents:nowComponents];
Then, you may want to check if thisWeekFriday is less than [NSDate date], and get the next Friday if it is. You can move date by one week with:
NSDateComponents *components = [[NSDateComponents alloc] init];
components.week = 1;
NSDate *nextWeekFriday = [gregorian dateByAddingComponents:components toDate:thisWeekFriday options:0];
You can calculate the number of days between two dates like this:
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSDayCalendarUnit
fromDate:today
toDate:futureDate
options:0];
NSInteger daysBetweenDates = dateComponents.day;
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];