how to use nsdateformatter to Venezuela - ios

I'm trying to schedule a date on the calendar, which has a start time and an end time, everything works fine if the time zone is in the U.S., and the date the change does not help and I need it to Venezuela what should I do as I place generica, to serve me anywhere no matter the time zone?
here I leave the code as data and bring me as I make the date format.
comes the date in the following format: 12/10/2012 05:00 pm
EKEvent* event = [EKEvent eventWithEventStore:eventStore];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter setDateFormat:#"dd/MM/yyyy hh:mm:ss a"];
horafin = [dateFormatter dateFromString:inicio];
horafin = [horafin dateByAddingTimeInterval:3600];
horainicio = [dateFormatter dateFromString:inicio];
[dateFormatter release];
NSString *ti=[[NSString alloc]initWithString:title];
NSLog(#"");
event.title =#"juego";
event.startDate = horainicio;
event.endDate = horafin;
event.URL = [NSURL URLWithString:#"http://pagina.com"];
event.notes = ti;
event.allDay = NO;
vc.event = event;
vc.editViewDelegate = self;
[self presentViewController:vc animated:YES completion:nil];
The event calendar call me correctly only fails when you change the time zone, as I did not take the date correctly I can do?

I use following code to get desired timezone-converted datetime:
- (NSString *) getUTCDate : (NSString *) inputDate
{
NSDateFormatter *utcDateFormatter = [[NSDateFormatter alloc] init];
[utcDateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm:ss Z"];
[utcDateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
// [utcDateFormatter setTimeZone :[NSTimeZone timeZoneForSecondsFromGMT: 0]];
// utc format
NSDate *dateInUTC = [utcDateFormatter dateFromString: inputDate];
return dateInUTC;
}
where dateInUTC is the date in GMT timezone.
You need to replace your own timezone in call [NSTimeZone timeZoneWithName:#"UTC"] and it should work.

Simply change one line of code in your code...
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
This will set time format in universal which doesnot change with region or day light saving time.

Related

Timezone issue when date display in UI(show 1 day lesser) in Objective C

I am facing one problem, which is related to different timezone.
In UI I have set the date with 3/11/18
leaveEndDate -> 3/11/18
in service I get result.
leaveEndDate = "2018-03-11T06:00:00Z";
But when I display the date in UI, its showing "3/10/18" but actual result should be "3/11/18"
It display 1 day lesser in the application.
I have change the Time Zone is -> Washington, DC - United States and it should work for all timezones.
Approach
//Adjust the dates.
NSDate* originalDateValue = [self valueForKey:actualKey];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss Z"; // tried formatter-> #"yyyy-MM-dd'T'HH:mm:ss'Z'"; // yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ
dateFormatter.timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
NSString *dateString = [dateFormatter stringFromDate:originalDateValue];
dateFormatter.timeZone = [NSTimeZone localTimeZone];
NSDate * actualDate = [dateFormatter dateFromString:dateString];
[self setValue:actualDate forKey:actualKey];
Another approach which I used is:
//Adjust the dates.
NSDate* originalDateValue = [self valueForKey:actualKey];
NSDate* adjustedDateValue = [self.class adjustDate:originalDateValue forTimezone:[self timeZoneName]];
+(NSDate*)adjustDate:(NSDate*)originalDateValue forTimezone:(NSString*)timezoneName
{
if([originalDateValue isEqual:[NSNull null]]) return nil;
NSTimeInterval timeInterval = ([[NSTimeZone timeZoneWithName:timezoneName] secondsFromGMTForDate:originalDateValue] - [[NSTimeZone localTimeZone] secondsFromGMTForDate:originalDateValue]);
NSDate* adjustedDateValue = [originalDateValue initWithTimeInterval:timeInterval sinceDate:originalDateValue]; //[originalDateValue dateByAddingTimeInterval:timeInterval];
return adjustedDateValue;
}
Third approach
NSDate* originalDateValue = [self valueForKey:actualKey];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSZ";
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
NSString *dateString = [dateFormatter stringFromDate:originalDateValue];
NSDate* adjustedDateValue = [dateFormatter dateFromString:dateString];
Any help?
originalDateValue is always being set to UTC time for the date you are giving it. NSDate works on UTC time since you aren't specifying a timezone. dateFormatter is converting the input you're giving it to your localtime, which is several hours behind. Since you don't specify a time, it's setting the time to 00:00:00 midnight, so when converting fro UTC to Washington DC, you're adjusting the timezone several hours behind.
You must adjust the source date according to the offset from UTC to localTimeZone:
NSInteger sourceUTCOffset = [sourceTimeZone secondsFromGMTForDate:originalDateValue];
NSInteger destinationUTCOffset = [localTimeZone secondsFromGMTForDate:originalDateValue];
NSTimeInterval interval = destinationUTCOffset - sourceUTCOffset;
NSDate destinationDate = [initWithTimeInterval:interval sinceDate:originalDateValue];
Then operate on destinationDate the way you have already provided.

Current Time in Date format shows wrong - ios

I am doing a prayer alarm app and i want to compare my current time with fetched time, but when i am getting my current time, it has some time difference always as shown;
// getting today's date string
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatterToday = [[NSDateFormatter alloc] init];
dateFormatterToday.timeZone = [NSTimeZone localTimeZone];
[dateFormatterToday setDateFormat:#"yyyy-MM-dd HH:mm a"];
NSString *currentDateTimeString = [dateFormatterToday stringFromDate:today];
// converting today's date string to NSDATE
NSDateFormatter *dateFormatterTodayFinal = [[NSDateFormatter alloc] init];
[dateFormatterTodayFinal setDateFormat:#"yyyy-MM-dd HH:mm a"];
dateFormatterTodayFinal.timeZone = [NSTimeZone localTimeZone];
NSDate *dateTodayFinal = [dateFormatterTodayFinal dateFromString:currentDateTimeString];
Here currentDateTimeString, which is in string format showing my current time as :
2016-01-11 17:52 PM (which is correct one)
but dateTodayFinal, which is in Date format shows:
2016-01-11 07:22:00 +0000
I have tested with different timezones, but the issue persist, please help some one. Thank you.
The second example is missing a stringFromDate call so the description method is probably used which uses it's own format, not the dateFormatterToday formatter. Also missing are the printing calls so we can only guess.
Add:
NSString *dateTodayFinalTimeString = [dateFormatterToday stringFromDate:dateTodayFinal];
NSLog(#"dateTodayFinalTimeString: %#", dateTodayFinalTimeString);
Output:
dateTodayFinalTimeString: 2016-01-11 07:57 AM
NSDate doesn't have any information about the timeZone, when you hover over the NSDate in xCode it will show you the time is in UTC.
If you want to convert the time back and forth, this information (timezone) has to be in the string you want to parse as well and set the timezone back to UTC:
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone localTimeZone];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSString *currentDateTimeString = [dateFormatter stringFromDate:today];
// converting today's date string to NSDATE
//
// NSDateFormatter *dateFormatterTodayFinal = [[NSDateFormatter alloc] init];
// [dateFormatterTodayFinal setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
dateFormatterToday.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate *dateTodayFinal = [dateFormatter dateFromString:currentDateTimeString];
Also have a look at the docs.

How to get correct string from NSDate across different time zones?

I need seven continuous dates' string from a single NSDate. This is what I did,
for (int i = 0; i < 7; i++) {
NSString *dayOfWeek = [self returnStringFromDate:currentDate withStringFOrmat:#"dd - EEEE"];
[daysOfWeekArray addObject:[dayOfWeek uppercaseString]];
currentDate = [currentDate dateByAddingTimeInterval:60 * 60 * 24];
}
- (NSString *)returnStringFromDate:(NSDate *)date
withStringFOrmat:(NSString *)stringFOrmat {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:locale];
[dateFormatter setDateFormat:stringFOrmat];
NSString *dateString = [dateFormatter stringFromDate:date];
return dateString;
}
I'm from India, If I have my own time zone(Indian standard time) and passing the currentDate as 01/sun/nov/2015 - it's logging as 2015-10-31 18:30:00 +0000, the above code works correctly. If I change my time zone to US time zone(CST), current date is logging as 2015-11-01 05:00:00 +0000, it's returning one date before for the string. If I try
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:stringFOrmat];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSString *dateString = [dateFormatter stringFromDate:date];
it's working,
but how can I identify which time time zone am I now?
Is it possible to use some common code to find the correct date string from the current time zone of the device?

Creating absolute time from NSDate

It looks easy, but I couldn't figure out a proper way to do this. I need to create an NSString from a NSDate which represents the same time on every device, independently from the iPhone's time zone settings.
Suppose userA is in London, where the actual time is 14:00, userB is in New York where is 9:00 and userC is in Hong Kong, where the actual time is 21:00.
NSDate *now = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yy HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:now];
Actually with this code I'm getting these results (when I log the dateString):
userA: 08/12/14 14:00:00
userB: 08/12/14 09:00:00
userC: 08/12/14 21:00:00
But I need to create dates like this
userA: 08/12/14 14:00:00
userB: 08/12/14 14:00:00
userC: 08/12/14 14:00:00
My goal is to create a "system/absolute time" which is the same inside the app and doesn't matter the original time zone of the user's device. The end result must look like this MM/dd/yy HH:mm:s.
Is it possible to get the NSDate *now = [[NSDate alloc] init]; from a pre-defined timezone? For example it could always use the actual time of the GMT-00 timezone.
I've tried to do it with this code, but when I run the code, the console writes out the wrong date (based on the device time zone setting) again, so I don't have a better idea. I would appreciate any ideas.
NSDate *now = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"Europe/London"];
[dateFormatter setDateFormat:#"MM/dd/yy HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:now];
NSLog(#"the date is %#,", dateString);
The below code should work. What ever the timezone you are in it will always display the time in UTC.
NSDate *now = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[[NSTimeZone alloc] initWithName:#"UTC"]];
[dateFormatter setDateFormat:#"MM/dd/yy HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:now];
NSLog(#"%#",dateString);
Between the following two threads, I think you'll find everything you need (and thensome). The first is an extensive example of a problem similar to yours (just remember to look at the code in the answers and not the question), while the second has all the time zone abbreviations that you'll ever need. Gotta love the helpful people on The Stack.
The links again, just in case
objective-c: Conversion between TimeZones
GMT timezone conversion in objective c
NSDate *localDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.dateFormat = #"MM/dd/yy";
NSString *dateString = [dateFormatter stringFromDate: localDate];
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc]init];
timeFormatter.dateFormat = #"HH:mm:ss";
NSString *dateString = [timeFormatter stringFromDate: localDate];

iOS: convert UTC timezone to device local timezone

I am trying to convert the following timezone to device local timezone:
2013-08-03T05:38:39.590Z
Please let me know how to convert it to local timezone.
How do I do it?
Time zones can be applied to NSDateFormatter, from which you can generate NSDate variables differentiated by the time difference.
NSString* input = #"2013-08-03T05:38:39.590Z";
NSString* format = #"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
// Set up an NSDateFormatter for UTC time zone
NSDateFormatter* formatterUtc = [[NSDateFormatter alloc] init];
[formatterUtc setDateFormat:format];
[formatterUtc setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
// Cast the input string to NSDate
NSDate* utcDate = [formatterUtc dateFromString:input];
// Set up an NSDateFormatter for the device's local time zone
NSDateFormatter* formatterLocal = [[NSDateFormatter alloc] init];
[formatterLocal setDateFormat:format];
[formatterLocal setTimeZone:[NSTimeZone localTimeZone]];
// Create local NSDate with time zone difference
NSDate* localDate = [formatterUtc dateFromString:[formatterLocal stringFromDate:utcDate]];
NSLog(#"utc: %#", utcDate);
NSLog(#"local: %#", localDate);
[formatterUtc release];
[formatterLocal release];
Try like this:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate* utcTime = [dateFormatter dateFromString:#"2013-08-03T05:38:39.590Z"];
NSLog(#"UTC time: %#", utcTime);
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:#"M/d/yy 'at' h:mma"];
NSString* localTime = [dateFormatter stringFromDate:utcTime];
NSLog(#"localTime:%#", localTime);
Hope it helps you....

Resources