Remove time zone offset from date in iOS - ios

I need to get the current NSDate.date and remove the time zone and parse it as a GMT date
NSDate.date
returns 2012-10-11 11:27:09 -0700
What I need is this: 2012-10-11 11:27:09 +0000

NSDate.date returns a date with the current date and time stored as GMT.
If you want to format the date as a string and show the GMT time, you should use a NSDateFormatter and set the locale to GMT:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"GMT"]];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSString *dateAsString = [formatter stringFromDate:[NSDate date]];

Related

There is differnce in date while I convert string to date

I am using following code to get date from string. All seems to be good in code but while I print the output date, there is difference of 12:30 hours in date.
What may be the issue? Am I missing something ?
NSString *strDate = #"8/22/2017 7:00:00 AM";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MM/dd/yyyy HH:mm:ss a"];
NSDate *date = [df dateFromString:strDate];
NSLog(#"%#", [date description]);
Output:
2017-08-21 18:30:00 +0000
The hour specifier is wrong, 12 hour format is hh
[df setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
Note:
Be aware that NSLog prints the date always in UTC although the date formatter considers the local time zone.
try this:
you just set the time to GMT
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

How to save two dates, one using local date and one in UTC

I have the need to store both local time as per users timezone and UTC time in a transaction table using core data
What I want is as follows
createDate should reflect the users local timezone, in this case I am in China so GMT+8
createDateUtc should represent the same timestamp but in UTC
I have tried various formatters but for some reason I cannot get the result I want
Here are my two formatters as you can see I tried using timezone as well but it still did not set the date to UTC.
NSDateFormatter *dateFormatterWithTz = [[NSDateFormatter alloc] init];
[dateFormatterWithTz setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
NSDateFormatter *dateFormatterUtc = [[NSDateFormatter alloc] init];
[dateFormatterUtc setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
//[dateFormatterUtc setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
here is how I create the date variables
NSDate *createdDateUtc = [NSDate date];
NSDate *createdDate = [NSDate dateWithTimeInterval:[[NSTimeZone systemTimeZone] secondsFromGMT] sinceDate:createdDateUtc];
Here is what the array looks like which is incorrect as you can see it created my Utc date as my local date and pushed the created date to tomorrow and still using the same time offset, the issue should be that my UTC date is set to local time but I cannot figure out how to make it UTC so that my created date gets calculated correctly from that
createdDate = "2016-12-25T03:19:38+08:00";
createdDateUtc = "2016-12-24T19:19:38+08:00";
you need to provide timeZone for date formater
for local timeZone try this
NSDateFormatter *formate =[[NSDateFormatter alloc] init];
[formate setTimeZone:[NSTimeZone localTimeZone]];
[formate setDateFormat:#"yyyy-MM-dd HH:mm:ss a"];
NSString *strToday =[formate stringFromDate:[NSDate date]];
and For UTC timezone set UTC timezone like this
NSDateFormatter *formate =[[NSDateFormatter alloc] init];
[formate setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[formate setDateFormat:#"yyyy-MM-dd HH:mm:ss a"];
NSString *strToday =[formate stringFromDate:[NSDate date]];
hope this will help you.

Convert NSString to NSDate, adding time

I have an NSString that is 05/08/2014. I want to convert that to an NSDate. However, I also need to add in time, so that the resulting NSDate looks like this:
Thu, 8 May 2014 00:00:00 -0500
The time is not important, I just need it to show midnight at the designated timezone.
I have tried:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss zzz"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:textdate.text];
[dateFormatter release];
NSLog(#"%#", dateFromString);
But the date comes back as (null).
Your date format is wrong for the first conversion. What you need is first to convert from string to date from one format and form the new date into the new string format. Something like this:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy"]; //convert the string into date (american time zone)
NSDate *theDate = [formatter dateFromString:textdate.text];
[formatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss ZZZ"];// as #Logan suggested
NSString *newDate = [formatter stringFromDate:theDate];
EEE, d MMM yyyy HH:mm:ss ZZZ
Your time zone code was lowercase instead of uppercase.
zzz corresponds to PDT
ZZZ corresponds to -0500
UTS 35

Not able to convert NSString into NSDate properly

I am having trouble with date formatter. I am setting date format and passing the date string in the appropriate way (i think). But the result log shows some other dateand the GMT value has been lost. What am I doing wrong here ? Can anyone help me out here ?
NSDateFormatter *newFormatter = [[[NSDateFormatter alloc] init] autorelease];
[newFormatter setDateFormat:#"yyyy-mm-dd hh:mm:ss zzz"];
NSDate *lObjDate = [newFormatter dateFromString:#"2012-11-05 01:45:03 GMT+05:30"];
NSLog(#">>>>> %#",lObjDate);
>>>>> 2012-01-04 20:15:03 +0000
You may try:
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss Z"];

Incorrect output from NSDateFormatter

I'm getting some strange output from a NSDateFormatter. I'm converting a date from GMT to the system time zone, which is EDT. This should be -4 hours from GMT.
-(NSDate*)parseDate:(NSString*)inStrDate {
NSLog(#"Date To Parse %#", inStrDate);
NSDateFormatter* dtFormatter = [[NSDateFormatter alloc] init];
[dtFormatter setLocale:[NSLocale systemLocale]];
[dtFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dtFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss +0000"];
NSDate* dateOutput = [dtFormatter dateFromString:inStrDate];
NSLog(#"Parsed Date %# %#", dateOutput, [NSTimeZone systemTimeZone]);
return dateOutput;
}
Log output:
2012-09-15 22:32:53.358 Date To Parse 2012-09-16 02:32:53 +0000
2012-09-15 22:32:53.360 Parsed Date 2012-09-16 06:32:53 +0000 America/New_York (EDT) offset -14400 (Daylight)
But it's returning +4 hours instead of -4. So where it should output 22.30 EDT (02.30 GMT) it's actually returning 06.30 EDT. Which is 8 hours in the future.
Can anyone help me understand if I'm going wrong somewhere here? I'm scratching my head but I can't figure out why this wont seem to work.
Thanks
You just got the dateFormatter the wrong way around.
When you do
[dtFormatter setTimeZone:[NSTimeZone systemTimeZone]];
you're telling it that the input Date is in terms of your time zone. Therefore, it converts it to GMT and outputs the time +4 hours. To do it the other way around you can use this:
NSDateFormatter* df_local = [[NSDateFormatter alloc] init];
[df_local setTimeZone:[NSTimeZone timeZoneWithName:#"America/New_York"]];
[df_local setDateFormat:#"yyyy.MM.dd' 'HH:mm:ss zzz"];
NSDate* dateOutput = [dtFormatter dateFromString:dateString];
NSLog(#"Parsed Date in GMT %#", dateOutput);
NSString *yourLocalDate = [df_local stringFromDate:dateOutput];
NSLog(#"in EDT %#",yourLocalDate);
This logs
2012-09-16 05:23:53.297 TestingApplication[10109:c07] Date To Parse 2012-09-16 02:32:53 +0000
2012-09-16 05:23:53.302 TestingApplication[10109:c07] Parsed Date in GMT 2012-09-16 02:32:53 +0000
2012-09-16 05:23:53.304 TestingApplication[10109:c07] in EDT 2012.09.15 22:32:53 EDT
if you want a date as a final output you can skip the secondary NSDateFormatter and just 'add' the difference between the UTC date and the local time.
formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *date = [formatter dateFromString:dateString]; // UTC date
NSDate *date2 = [date dateByAddingTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMTForDate:date]]; // local date!

Resources