Validation of date and month - ios

i have a current date using NSDate which is ma start date....and i add 4 more days to the current date where i get ma endDate..
NSString *StrtDate= [dateFormatter stringFromDate:[NSDate date]];
int daysToAdd = 4;
NSDate *newDate1 = [[NSDate date] dateByAddingTimeInterval:60*60*24*daysToAdd];
NSString *StopDate= [dateFormatter stringFromDate:newDate1];
suppose the current date is todays date..thats 28th of jan the end date becomes 32nd of Jan which is invalid rite?
how do u validate the date??

Have you run the code and checked whether the resultant date is indeed 32nd January?
The framework is intelligent enough to understand valid dates. You will get correct date.

You don't need to worry about this as the date framework knows how many days are in each month.

Related

NSDateFormatter returning null value

I'm trying to format a date using NSDateFormatter however for some dates, formatted the same way, it returns the time without the first digit, and for others it returns null.
NSDateFormatter *dateFormat = [NSDateFormatter new];
[dateFormat setDateFormat:#"EEEE dd/MM/yyyy HH:mm"];
dateFormat.locale = [NSLocale localeWithLocaleIdentifier:#"en_AU"];
NSDate *startDate = [dateFormat dateFromString:startString];
NSDate *endDate = [dateFormat dateFromString:endString];
This date: "Saturday 1/21/2017 17:00" will return (null)
This date: "Thursday 2/9/2017 14:00" will return 2017-09-02 04:00:00 +0000
This date: "Thursday 2/9/2017 20:30" will return 2017-09-02 10:30:00 +0000
Can anyone shed some light on where I am going wrong.
Thanks
EEEE dd/MM/yyyy HH:mm to EEEE MM/dd/yyyy HH:mm. Just a small mistake inverting days and months place.
Because, clearly "Saturday 1/21/2017 17:00", means the 21th of January, because the 21th month in a year doesn't exist (at least, not here).
For the 10h difference, it's due to time zones. In en_AU (east coast I guess), there is a 10h difference from GMT.

Issues in converting date string (any time zone) to NSDate of French Time Zone

I want to convert a date string (can be in any time zone) to a date in French Time Zone. I am using following code.
NSString * dateString = #"27/05/2015 - 19:00" // system time zone is GMT +5
NSDateFormatter* frenchDateFormatter = [[NSDateFormatter alloc] init];
[frenchDateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Paris"]];
[frenchDateFormatter setDateFormat:#"dd/MM/yyyy - HH:mm"];
NSDate *frenchDate = [frenchDateFormatter dateFromString:dateString];
NSLog(#"%#",frenchDate);
NSString * frenchString = [frenchDateFormatter stringFromDate:frenchDate];`
Elaboration
--> System time zone is GMT +5
--> French time zone is GMT +2
Date string = 27/05/2015 - 19:00
Expected result = 27/05/2015 - 16:00
Actual result (NSDate) = 2015-05-27 17:00:00 +0000
Actual result (NSString from date) = 27/05/2015 - 19:00
Kindly point out if I am missing something
If you use NSLog to display dates it'll be displayed in UTC. So either you have to convert in your head, or don't use it. I wrote a long answer explaining this to a different question.
Because you have set the timezone of your parsing dateFormatter to Paris the string you parse is treated as "time in paris". That's your problem, you actually wanted to parse it in local time.
The results you get are exactly as one would expect.
You create a NSDate that relates to "19:00 in Paris". Since Paris is UTC+2 that date is 17:00 in UTC (or in +0000). If you convert that date back to "time in Paris" you end up with the same string as before.
If you want to convert the representation of a point in time in your location to a different representation at a different location you have to use two dateFormatters.
NSString *localDateString = #"27/05/2015 - 19:00" // system time zone is GMT +5
NSDateFormatter* localDateFormatter = [[NSDateFormatter alloc] init];
[localDateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[localDateFormatter setDateFormat:#"dd/MM/yyyy - HH:mm"];
NSDate *date = [localDateFormatter dateFromString:localDateString]; // date contains point in time. It no longer has a timezone
NSDateFormatter* franceDateFormatter = [[NSDateFormatter alloc] init];
[franceDateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Paris"]];
[franceDateFormatter setDateFormat:#"dd/MM/yyyy - HH:mm"];
NSString * timeInFranceString = [franceDateFormatter stringFromDate:date]; // representation of the point in time from above for people in Paris
This line prints out the date/time in GMT, as it calls [NSDate description], and there is a potential difference between systemTimeZone and GMT, hence the difference you are seeing:
NSLog(#"%#",currentDate);
If you want to see what the date/time is for a particular timezone then use the NSDateFormatter object to get the string.
A date doesn't have a time zone information. A date is internally represented as a number. We don't have to know anything about that number (it's a number of seconds from a fixed date in UTC), the important thing is to understand that to display a date to a user, you have to convert it to a string first.
A string representation of a number is generated from a date using a date format and a time zone. For all date -> string and string -> date conversions you can use NSDateFormatter.
You have successfully parsed currentDate from your string representation. If you want to reverse the process and get the string representation, just use [currentDateFormatter stringFromDate:currentDate]
Check at http://www.timeanddate.com/worldclock/
Right now Paris is two hours ahead of UTC. The result is absolutely correct. NSDate keeps dates in UTC. The idea is that if any two people look at their watch at the same moment, and convert the time they see on their watch to NSDate, they will get the same result.
You cannot get an NSDate for a timezone. NSDate doesn't support time zones. The only way to get a date with a time zone is to use NSDateFormatter to convert it to a string.

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 ?

How to show date with timezone for date, relevantly to defined country/zone?

I need to show a date in concrete time zone including DST (European time). App will be used in Lithuania, so time zone is +3 at summer and +2 at other time. The thing is, I have just a list of dates and I don't know how to show +3 for summer dates and +2 for other dates. Currently, I have time zones:
// Eastern European Summer Time UTC + 3 hours
NSTimeZone *timeZoneWithDst = [NSTimeZone timeZoneWithAbbreviation:#"EEST"];
//Eastern European Time UTC + 2 hours
NSTimeZone *timeZoneWithoutDst = [NSTimeZone timeZoneWithAbbreviation:#"EET"];
But how to loop through my list of dates and calculate should I add +3 or +2 to date?
UPDATE Finally I got it working by applying Martin R. suggestion to use time zone by name, not by abbreviation. In this way, date with this time zone handles DST automatically. Here's my code for converting dates:
NSTimeZone *TimeZone = [NSTimeZone timeZoneWithName:#"Europe/Vilnius"];
NSInteger seconds = [myTimeZone secondsFromGMTForDate:someDate];
NSDate *result = [NSDate dateWithTimeInterval:seconds sinceDate:someDate];
To convert an NSDate to a string representation, use NSDateFormatter. By default, it uses the local time zone. To display the date according to a concrete time zone, you can set
NSTimeZone *tz = [NSTimeZone timeZoneWithName:#"Europe/Vilnius"];
[dateFormatter setTimeZone:tz];
(According to http://www.timezoneconverter.com/cgi-bin/findzone, the time zone for Lithuania is "Europe/Vilnius".)
This is a very similar alternative that worked for me, in Swift:
var currentDate: NSDate {
let currentLocalTime = NSDate()
let localTimeZone = NSTimeZone.systemTimeZone()
let secondsFromGTM = NSTimeInterval.init(localTimeZone.secondsFromGMT)
let resultDate = NSDate(timeInterval: secondsFromGTM, sinceDate: currentLocalTime)
return resultDate
}

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.

Resources