Incorrect time converting - ios

I need to convert time which I get from server to users time.
I use This code.
- (NSString *)convertServerTime:(NSString *)serverTime
{
NSDateFormatter *serverFormatter = [[NSDateFormatter alloc] init];
[serverFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
[serverFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *theDate = [serverFormatter dateFromString:serverTime];
NSDateFormatter *userFormatter = [[NSDateFormatter alloc] init];
[userFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
[userFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString *dateConverted = [userFormatter stringFromDate:theDate];
return dateConverted;
}
system time zone: (Europe/London (GMT) offset 0)
serverTime: 2014-08-11 00:00
dateConverted: 2014-08-11 01:00
But I need to get the same time as serverTime. Instead this I receive +1 houre. Please help me.

Related

How to convert UTC date string to local time

I currently get time as UTC and I want it to be convert into local time zone
NSDate * startDate;
Example strDate - 14/9/2017 7.28 Am
Required format - 14/9/2017 1.52 pm
I need help with objective c.
NSDate * startDate ; //contain utc date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, MMM d, yyyy - h:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *timestamp = [dateFormatter stringFromDate:startDate];
but this is not correct time but date is correct
First convert NSString to NSDate.
NSDateFormatter *DateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZoneEDT =[NSTimeZone timeZoneWithName:#"GMT"];
[DateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[DateFormatter setTimeZone:timeZoneEDT];
[DateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.SSS"];
NSDate *CurrentDate = [DateFormatter dateFromString:DateStr];
Then convert GMT time to local time.
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:#"Asia/Kolkata"];
[dateFormatter setTimeZone:sourceTimeZone];
NSString *dateRepresentation = [dateFormatter stringFromDate:dateFromString];
#athira try this. it works for me.
NSDate *startDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/M/yyyy h.mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *timestamp = [dateFormatter stringFromDate:startDate];
NSLog(#"date = %#",timestamp); //date = 14/9/2017 4.34 PM
it will print current time with proper GMT+5:30 and i have used localTimeZone.

How to get the UTC time from NSDate iOS

I want to log the server time in my iOS app , I am using NHNetworkTime library to get the network time, when I try to print the following code its showing GMT, through I have chosen UTC, how to print the date time in UTC?
NSDate* datetime = [NSDate networkDate];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]]; // Prevent adjustment to user's local time zone.
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.SSS Z z "];
NSString* dateTimeInIsoFormatForZuluTimeZone = [dateFormatter stringFromDate:datetime];
NSLog(#"%#",dateTimeInIsoFormatForZuluTimeZone);
this is printing 2016-03-09 06:51:23.406 +0000 GMT
There is no time difference between Coordinated Universal Time (UTC) and Greenwich Mean Time(GMT). you can get more info from here More info
Refer :-
-(NSString *)getUTCFormateDate:(NSDate *)localDate
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:localDate];
[dateFormatter release];
return dateString;
}
Moreover to get time
-(NSString *)getTimeFromDate:(NSDate *)localDate
{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init]autorelease];
dateFormatter.dateFormat = #"MM/dd/yy";
NSString *dateString = [dateFormatter stringFromDate: localDate];
NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc]init]autorelease];
timeFormatter.dateFormat = #"HH:mm:ss";
return [timeFormatter stringFromDate: localDate];
}

Displaying time from Date string

I have a time string in the following format(ISO 8601):
start = "1900-01-01T11:30:00"
stop = 1900-01-01T21:30:00
From the above string I need to display time like this :
Starting Time : 11:30a.m
Stopping Time : 09:30p.m
For that I had created an NSDate using below code, But I think it displays time in GMT format.
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
date = [dateFormatter dateFromString:opening];
stopTime = date;
//Output of above code : Date = 1900-01-01 15:36:40 +0000
How can I obtain the Starting and Ending time string from above ISO 8601 time string? Do I need to depend on String operations rather than using iOS NSDateformatter?
Try this,
NSString *start = #"1900-01-01T11:30:00";
NSString *stop = #"1900-01-01T21:30:00";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *sDate = [dateFormatter dateFromString:start];
NSDate *eDate = [dateFormatter dateFromString:stop];
[dateFormatter setDateFormat:#"hh:mm a"];
start = [dateFormatter stringFromDate:sDate];
stop = [dateFormatter stringFromDate:eDate];
NSLog(#"Starting Time : %#", start);
NSLog(#"Stopping Time : %#", stop);
And the outputs will be
2014-02-12 10:55:13.931 [950:a0b] Starting Time : 11:30 AM
2014-02-12 10:55:13.932 [950:a0b] Stopping Time : 09:30 PM
Check this
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *currentDate = [NSDate date];
NSString *iso8601valueStr = [dateFormatter stringFromDate:currentDate];

iOS : Switching between 12/24 hour times from strings

Interesting issue thats caught me out.
I receive string times from a server to a device. which I then convert in to a NSDate.
When the device was set to displaying 24hour times, life was good.
Now I am testing it on a device set to 12hour times. everything has stopped working. Dates are coming back as null
I first had
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"HH:mm"];
self.startTime = [dateFormat dateFromString:(NSString *)self.startTime];
Worked perfectly for devices showing 24hour dates but not 12hour.
I then tried
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"hh:mm"];
self.startTime = [dateFormat dateFromString:(NSString *)self.startTime];
This works fine up until 12 noon, then all dates are returned as null
Update
I have also tried adding "a" but this still results in returning null
if (startDate == nil)
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"hh:mm a"];
startDate = [dateFormat dateFromString:(NSString *)self.startTime];
}
Update 2
Adding local, adding :ss adding a all still do not work
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *twelveHourLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
dateFormat.locale = twelveHourLocale;
[dateFormat setDateFormat:#"hh:mm a"];
startDate = [dateFormat dateFromString:(NSString *)self.startTime];
It's close... I think you need:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:locale];
[dateFormatter setDateFormat:#"HH:mm"];
startDate = [dateFormatter dateFromString:(NSString *)self.startTime];
You can try this
12 to 24 hour format
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:#"hh:mm a"];
NSDate* newDate = [df dateFromString:LocationTrackingStartTime];
[df setDateFormat:#"HH:mm"];
newDate = [df stringFromDate:newDate];
24 to 12 hour format
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:#"yyyy-mm-dd hh:mm:ss"];
NSDate* newDate = [df dateFromString:[df stringFromDate:[NSDate date]]];
[df setDateFormat:#"hh:mm a"];
newDate = [df stringFromDate:[NSDate date]];
You have to add am/pm:
NSLocale *twelveHourLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
dateFormat.locale = twelveHourLocale;
[dateFormat setDateFormat:#"hh:mm: a"];
Change 24 hours format to 12 hour format,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//Set 24 hours date format
dateFormatter.dateFormat = #"HH:mm";
//Here #"your24hours date string" is your string date with 24 hours format
NSDate *date = [dateFormatter dateFromString:#"your24hours date string"];
//Set 12 hours date format
dateFormatter.dateFormat = #"hh:mm a";
NSString *strDate = [dateFormatter stringFromDate:date];
NSLog (#"%#", strDate);
//Output is in 12 hours date format.
You wanted to convert an NSDate forced to NSString to an NSDate again.
First you have to convert it to and NSString, not forcing it!
I also support 12 and 24 hour formats and i have no problem with it.
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:#"MM/dd/yyyy hh:mm a"];
NSDate* newDate = [df dateFromString:strDate4Convert];
[df setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
NSString *newTimeStr = [df stringFromDate:newDate];
You just need to set the locale to take the am pm format. And everything else as usual.
NSDateFormatter * df = [[NSDateFormatter alloc]init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[df setLocale:locale];
[df setDateStyle:NSDateFormatterNoStyle];
[df setTimeStyle:NSDateFormatterShortStyle];
NSLog(#"%#", [df stringFromDate:pickUpDate]);

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