Incorrect Week StartDate with NSCalendar and NSDateComponent - ios

My Problem, in my app i used to show event for each week (week starts with monday), on next button i will be calculating next week dates, my working was working fine , but for a specific day it not returning proper value ,
following is my code
with current week end date , i will add one day to it to get next week start date
NSDateComponents *dateComponet = [[NSDateComponents alloc] init];
[dateComponet setDay:days];
return [[self getCustomCalendar] dateByAddingComponents:dateComponet toDate:date options:0];
where [self getCustomeCalendar] is,
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setLocale:[NSLocale currentLocale]];
[gregorian setFirstWeekday:2]; // 2 monday
return gregorian;
following are the return value for some weeks,
weekend Date: 2014-02-23 00:00:00 +0000 , next weekStartDate : 2014-02-24 00:00:00 +0000
weekend Date: 2014-03-02 00:00:00 +0000 , next weekStartDate : 2014-03-03 00:00:00 +0000
weekend Date: 2014-03-09 00:00:00 +0000 , next weekStartDate : 2014-03-10 00:00:00 +0000
weekend Date: 2014-03-16 00:00:00 +0000 , next weekStartDate : 2014-03-17 00:00:00 +0000
weekend Date: 2014-03-23 00:00:00 +0000 , next weekStartDate : 2014-03-24 00:00:00 +0000
// this is the problem one
weekend Date: 2014-03-30 00:00:00 +0000 , next weekStartDate : 2014-03-30 23:00:00 +0000
as you can see all dates are returning properly by adding one day,
but for 2014-03-30 , when +1 day is added, it should return 2014-03-31 instead, it is returned with 23 hours, i couldn't able to find the cause for this issue
any idea why this is happening ?

Okay even though i couldn't resolve the daylight saving issue, i was able to solve my problem by calculation like below,
Usually i will take the calculated value as week start date, but now what am doing is i will calculate the week start date (current week endDate + 1 ), then will do another calculation to find out what the actual start date for that particular week using NSCalendar (with GMT) which resolves this problem, thanks for the comments and suggestions,
if someone finds better solution, please post it.

Related

iOS 'dateFromString' Returning different?

I am using the NSDateFormatter's dateFromString, yet when it is given the string, it spits out different hours.
code
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
dateformatter.dateFormat = #"yyyy-MM-dd";
NSDate *firstDate = [dateformatter dateFromString:#"2017-06-02"];
NSDate *secondDate = [dateformatter dateFromString:#"2016-11-02"];
output
firstDate: 2017-06-01 22:00:00 +0000
secondDate: 2016-11-01 23:00:00 +0000
I wonder why they do not have the same hour?
You did not specify a time for the two dates, so they were initialized to midnight, local time. When you printed them they were displayed in UTC. Your local time zone (Central European Time) observed Daylight Savings Time from March 27, 2016 to October 30, 2016. Therefore the June midnight date, which fell under Daylight Savings, is 2 hours ahead of UTC (22:00:00), while the November midnight date, which did not fall under Daylight Savings, is only 1 hour ahead of UTC (23:00:00).

CMMotionActivity startDate is 1970 not 2016

My CMMotionActivity object has the property timestamp and startDate.
Printing both of these shows the value is from 1970, not 2016 as expected.
[self.motionActivityManager startActivityUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMMotionActivity *activity) {
NSLog(#"startDate = %#",activity.startDate);
NSLog(#"timestamp = %f",activity.timestamp);
}];
2016-07-20 17:46:53.294 MyApp[292:30246] timestamp = 8648.852289
2016-07-20 17:46:54.229 MyApp[292:30246] startDate = 1970-01-01 02:24:09 +0000
My device's time and date is set correctly, though it has been turned off for weeks.
EDIT: Today I got this: No code changes.
2016-07-21 11:26:34.292 MyApp[229:7473] startDate = 2016-07-21 10:26:34 +0000
2016-07-21 11:26:34.293 MyApp[229:7473] timestamp = 662.524030
Eh... Close enough. "timestamp" (from NSLogItem) is a NSTimeInterval so a phone running time or something.
I guess 24hrs or a phone reboot fixes the issue.

Unix time stamp received from server using EEST timezone

In my application i request certain date field from my server, the server returns values like :2014-06-03 00:00:00 EEST , but i need to load that data using GMT(GMT+2…) format, i am using the following code :
double unixTimeStamp = [[date objectAtIndex:indexPath.row] doubleValue];
NSTimeInterval _interval=unixTimeStamp;
NSDate *dateToFinal = [NSDate dateWithTimeIntervalSince1970:_interval];
NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
_formatter.dateFormat = #"yyyy-MMMM-dd HH:mm:ss zzz";
[_formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:[[NSUserDefaults standardUserDefaults] objectForKey:#"timeZone"]] ];
[_formatter setDateFormat:#"dd MMMM yyyy"];
dateFinal=[_formatter stringFromDate:dateToFinal];
// NSLog(#"Date Time < 3 %#" , dateFinal);
the user has the possibility to change his timezone, that's why when he saves it it's stored in a local cached variable , which i call in the previous code. But the most weird part is , if the user changes to GMT +1 or +2 or whatever, the application would still output the same hour time, which is 00:00, but if he changes to anything different from GMT, it will change. It's not normal to have GMT,GMT+1,GMT+4… to output the same hour which is 00:00. As a unix timestamp example we have 1401742800, which if u use an online time converter you would get Mon, 02 Jun 2014 21:00:00 GMT , but in the app its neither the same date nor time…what am i missing ?

Bug in dateByAddingTimeInterval

After going almost crazy searching where my code failed ... I was able to isolated this strange behaviour. Look at what hapens when substracting -200 days
NSDate *now = [NSDate date]; //now is 2013-07-19
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"YYYY-MM-dd"];
NSDate *newDate1 = [now dateByAddingTimeInterval:60*60*24*-199];
newDateTmp=[format stringFromDate:newDate1];
NSLog(#"now:%# newDateTmp:%#",now,newDateTmp);
newDate1 = [now dateByAddingTimeInterval:60*60*24*-200];
newDateTmp=[format stringFromDate:newDate1];
NSLog(#"now:%# newDateTmp:%#",now,newDateTmp);
newDate1 = [now dateByAddingTimeInterval:60*60*24*-201];
newDateTmp=[format stringFromDate:newDate1];
NSLog(#"now:%# newDateTmp:%#",now,newDateTmp);
newDate1 = [now dateByAddingTimeInterval:60*60*24*-365];
newDateTmp=[format stringFromDate:newDate1];
NSLog(#"now:%# newDateTmp:%#",now,newDateTmp);
logs:
2013-07-19 15:58:46.123 Vendes[2927:907] now:2013-07-19 13:58:46 +0000 newDateTmp:2013-01-01 // This is OK
2013-07-19 15:58:46.124 Vendes[2927:907] now:2013-07-19 13:58:46 +0000 newDateTmp:2013-12-31 // This is INCORRECT!!!! Look at the YEAR
2013-07-19 15:58:46.125 Vendes[2927:907] now:2013-07-19 13:58:46 +0000 newDateTmp:2012-12-30 // This is OK
2013-07-19 15:58:46.127 Vendes[2927:907] now:2013-07-19 13:58:46 +0000 newDateTmp:2012-07-19 // This is OK
I guess it will hapopen also tomorrow substracting 201 .. ?? :)
Any idea?
Thks
PS. I solved it using
NSDate *newDate1= [NSDate dateWithTimeInterval:60*60*daysToAdd sinceDate:now];
that works for any daysToAdd value.
It is not a bug. The format you use is wrong. It should be
#"yyyy-MM-dd"
with lower-cased y
More information on upper-cased Y :
Y - Year (in "Week of Year" based calendars). This year designation
is used in ISO year-week calendar as defined by ISO 8601, but can be
used in non-Gregorian based calendar systems where week date
processing is desired. May not always be the same value as calendar
year.

Convert number to date time

I'm not a programmer. I just needed to get some calendar info out of my iphone backup. I got the data out into a SQL Lite database but the dates are in a number format that I don't understand and I can't find a way to convert them to a format I can read. Can someone show me how to convert the numbers or it might be easier to just convert them for me. The numbers are:
352425600
353718000
356054400
357350400
358560000
359769600
Thanks
I wonder if these are in epoch time? try using this calculator and see if the results look possible to you.
EDIT:
nevermind, this answer has what you are looking for.
These numbers are stores as a time difference in seconds to the date 1. January 2001 GMT. You can calculate the date by adding the numbers to this date if you treat them as seconds.
If you can use Objective-C you can use the method: dateWithTimeIntervalSinceReferenceDate
Here is a short loop for calculating the dates:
NSArray *dates = #[#352425600, #353718000, #356054400, #357350400, #358560000, #359769600];
for (NSNumber *timeInterval in dates) {
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:[timeInterval integerValue]];
NSLog(#"timeInterval:%# = date:%# ", timeInterval, date);
}
And here is the result:
timeInterval:352425600 = date:2012-03-03 00:00:00 +0000
timeInterval:353718000 = date:2012-03-17 23:00:00 +0000
timeInterval:356054400 = date:2012-04-14 00:00:00 +0000
timeInterval:357350400 = date:2012-04-29 00:00:00 +0000
timeInterval:358560000 = date:2012-05-13 00:00:00 +0000
timeInterval:359769600 = date:2012-05-27 00:00:00 +0000

Resources