NSDate from NSString adjusted to device timzone - iphone

I have an NSString like this :
NSString *playTime = #"20:45";// GMT
I need to convert this string to NSDate (only hours and seconds) then adjusted to device timezone.
Example:
If user device timezone is set to GMT, I need a result like this : #"20:45";
If user device timezone is set to GMT + 2, I need a result like this : #"22:45";

Try this
NSString *gmtDateString = #"20:45";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[dateFormatter setDateFormat:#"HH:mm"];
//Now you have date in GMT time zone by default dateFormatter timezone would be in local time zone
NSDate *date = [dateFormatter dateFromString:dateString];
//set to local time zone
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
//Now you have string in localDate format
NSString *localDateString = [dateFormatter stringFromDate:date];

The below code will calculate the time w.r.t. local time zone.
Suppose my time zone is GMT+05:30 so the time 05:30 will become 00:00.
NSString *playTime = #"20:45";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"HH:mm";
NSDate *date = [formatter dateFromString:playTime];
NSLog(#"%#",date);
Log is: 2000-01-01 15:15:00 +0000

Use this code for date conversion as per your requirement.
NSDateFormatter *dateFormat = [NSDateFormatter new];
[dateFormat setDateFormat:#"HH:mm"];
[dateFormat setTimeZone:[NSTimeZone localTimeZone]];
logDate = [dateFormat dateFromString:#"20:45"];
NSLog(#"Date %#",[dateFormat stringFromDate:logDate]);

Related

Issue in coverting GMT time to Local time

My current Device time is --- 2013-05-09 10:23 AM
I convert it int GMT with following code
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm a";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"time in GMT ------ %#",timeStamp);
And I got the out put as
time in GMT ------ 2013-05-10 04:53 AM
then I found my time zone
NSTimeZone *localTime = [NSTimeZone systemTimeZone];
NSLog(#"local timezone is------ %#",[localTime name]);
out put
local timezone is------ Asia/Kolkata
Now I need to convert the above GMT time to local time.I used the following code
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:[localTime name]];
[dateFormatter setTimeZone:sourceTimeZone];
NSDate *dateFromString = [dateFormatter dateFromString:timeStamp];
NSLog(#"local time is ------- %#",dateFromString);
But I am getting a wrong out put..
local time is ------- 2013-05-09 19:23:00 +0000
Why this happen ?
I should get local time as - 2013-05-09 10:23 AM
Your timestamp string is in GMT. This means that when you want to convert the GMT string back to an NSDate, your date formatter needs to be set to GMT, not your local timezone. By doing this you will get the proper NSDate.
And don't forget that when you log an NSDate object, it will display in GMT regardless of anything else.
// get current date/time
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *currentTime = [dateFormatter stringFromDate:today];
[dateFormatter release];
NSLog(#"User's current time in their preference format:%#",currentTime);
Please my dear do little effort on google means first search your question on google and if you don't get your answer in that case put your question on stackover flow.
And see your answer on below link....
iPhone: NSDate convert GMT to local time
iphone time conversion gmt to local time
Date/time conversion to user's local time - issue
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm a";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"time in GMT ------ %#",timeStamp);
NSTimeZone *localTime = [NSTimeZone systemTimeZone];
NSLog(#"local timezone is------ %#",[localTime name]);
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:sourceTimeZone];
NSDate *dateFromString = [dateFormatter dateFromString:timeStamp];
NSLog(#"local time is ------- %#",dateFromString);
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
dateFormatter1.dateFormat = #"yyyy-MM-dd HH:mm";
NSTimeZone *gmt1 = [NSTimeZone localTimeZone];
[dateFormatter1 setTimeZone:gmt1];
NSString *timeStamp1 = [dateFormatter1 stringFromDate:[NSDate date]];
NSLog(#"local time is ------- %#",timeStamp1);
Output:
time in GMT ------ 2013-05-10 05:13 AM
local timezone is------ Asia/Kolkata
local time is ------- 2013-05-10 00:13:00 +0000
local time is ------- 2013-05-10 10:43
Try Using This Code :
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm a"];
// get time zone
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSDate *date = [NSDate date];
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
date = [dateFormatter dateFromString:timeStamp];
// change time zone
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"Asia/Kolkata"]];
// or you can use SystemTimeZone
// [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString *strDateWithLocalTimeZone = [dateFormatter stringFromDate:date];
NSLog(#"Local Time Zone Date %#", strDateWithLocalTimeZone);

Convert GMT time to local time

I got current GMT time - 2013-05-15 08:55 AM
my current local time is - 2013-05-15 02:06 PM
and time zone is - Asia/Kolkata
I tried to convert the above GMT to my local time with this code
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"Asia/Kolkata"];
[dateFormatter setTimeZone:sourceTimeZone];
// timeStamp - is the above GMT time
NSDate *dateFromString = [dateFormatter dateFromString:timeStamp];
NSLog(#"local time is %#",dateFromString);
but this give me wron time as - 2013-05-14 19:04:00 +0000 instead of 2013-05-14 02:06 PM
Why this happens?
please help me to clear this.Thanks in advance
Nothing is going wrong, a NSDate instance will never have a timezone ( well it will but it is always GMT, indicated by the +0000 timezone in the date description).
What you are telling dateformatter in your code is that the timezone of the date you want to parse is Asia/Kolkata but you want it to be GMT.
First you need to make a NSDate object from the input date string with the time zone set to the correct time zone, GMT as you indicated.
NSTimeZone *gmtTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:sourceTimeZone];
NSDate *dateFromString = [dateFormatter dateFromString:timeStamp];
NSLog(#"Input date as GMT: %#",dateFromString);
Then when you present the date as an string use :
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:#"Asia/Kolkata"];
[dateFormatter setTimeZone:sourceTimeZone];
NSString *dateRepresentation = [dateFormatter stringFromDate:dateFromString];
NSLog(#"Date formated with local time zone: %#",dateRepresentation);
The shortest way that I found to convert GMT time to your local timezone is
NSDate* localDateTime = [NSDate dateWithTimeInterval:[[NSTimeZone systemTimeZone] secondsFromGMT] sinceDate:dateInGMT];
Furthermore, to convert this date value into string
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm:ss"];//use the formatted as per your requirement.
//Optionally for time zone converstions
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"..."]];
NSString *localTimeDateString = [formatter stringFromDate:localDateTime];
NSLog(#"local time is %#",[dateFormatter stringFromDate: dateFromString]);
NSTimeZone* gmtTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSTimeZone* localTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"Asia/Kolkata"];
NSDateFormatter *gmtDateFormatter = [[NSDateFormatter alloc] init];
[gmtDateFormatter setTimeZone:gmtTimeZone];
NSDate *date = [gmtDateFormatter dateFromString:timestamp];
NSDateFormatter *localDateFormatter = [[NSDateFormatter alloc] init];
[localDateFormatter setTimeZone:localTimeZone];
NSLog(#"local time is %#",[localDateFormatter stringFromDate:date]);
Use timeZoneWithName
NSDate *date=[[NSDate alloc]init];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setTimeZone:[NSTimeZone timeZoneWithName:#"Asia/Kolkata"]];
[timeFormat setDateFormat:#"HH:mm"];
NSString *dateStr=[timeFormat stringFromDate:date];

How to get my time in UTC for iPhone project?

How to get my country time in UTC for iPhone development?
Here is the Code, Just Call below method when you want to set TimeZone and Date Format.
-(void)setDateFormat
{
NsDate myDate = [NSDate date];//here it returns current date of device.
//now set the timeZone and set the Date format to this date as you want.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:timeZone];
NSString *newDate = [dateFormatter stringFromDate:myDate];
// here you have new Date with desired format and TimeZone.
}
-(NSString *)UTCFormDate:(NSDate *)myDate
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:timeZone];
NSString *dateString = [dateFormatter stringFromDate:myDate];
return dateString;
}
Source:
https://stackoverflow.com/a/2615847/857865
[NSDate date] gives you the current point in time, which may be printed in UTC, GMT, EST or any other timezone. (Representation-wise, the current point in time is the number of seconds since a reference date in UTC.)
To get the current point in time in UTC as a string, get an NSDateFormatter and configure it to output a timestamp using the UTC timezone.

NSDateFormatter and Time Zone issue?

I have a string with time in GMT and i want to make it according to the system time zone but its not working properly -
NSLog(#"Time Str = %#",Time);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-MM-yyyy hh:mm a"];
[NSTimeZone resetSystemTimeZone];
NSLog(#"system time zone = %#",[NSTimeZone systemTimeZone]);
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *date = [dateFormat dateFromString:Time];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
NSLog(#"date from string = %#",date);
NSLog(#"string from date = %#",[dateFormat stringFromDate:date]);
Output at console -
////////////////////////////////////////////////////////////////////////////////////////////////
Time Str = 09-12-2011 07:57 AM
system time zone = Asia/Calcutta (IST) offset 19800
date from string = 2011-12-09 02:27:00 +0000
string from date = 09-12-2011 07:57 AM
////////////////////////////////////////////////////////////////////////////////////////////////
Calcutta is +5:30 from GMT, so the time in date should be 1:27 but its showing 02:27. Also when i take a string from this date its showing me the same sting that i used to make date, i want the string updated according system time zone.
Thanks
If the date string is in GMT you can't use your system Timezone to create the NSDate from the NSString.
replace the first occurrence of [dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
with [dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];

Problem in setting nsdate value from a string

NSDate *My_StartDate,*My_EndDate ;
NSDateFormatter * df= [[NSDateFormatter alloc]init];
[df setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
My_StartDate = [df dateFromString:#"01/05/2010 10:15:33"];
My_EndDate = [df dateFromString:#"01/05/2010 10:45:33"];
NSLog(#"%#",My_StartDate);
NSLog(#"%#",My_EndDate);
In the log i get something like this for the my_startdate as 2010-05-01 04:45:33 +0000 and end date as 2010-05-01 05:15:33 +0000 instead i should have got value as for start date as 2010-05-01 10:15:33 +0000 and end date as 2010-05-01 10:45:33 +0000
Try with below function:
-(NSString *)getDateStringFromDate :(NSDate *)dateValue{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setTimeStyle:NSDateFormatterShortStyle];
[timeFormat setDateFormat:#"HH:mm:ss"];
//[timeFormat setDateFormat:#"HH:mm"];
//[timeFormat setDateFormat:#"HH:mm a"];
////
NSString *theDate = [dateFormat stringFromDate:dateValue];
NSString *theTime = [timeFormat stringFromDate:dateValue];
NSLog(#"\n"
"theDate: |%#| \n"
"theTime: |%#| \n"
, theDate, theTime);
return theDate;
}
Change Format of data as per your need.
Let me know in case of any difficulty.
Cheers.
This shows date which follow American standard time string but by this reason you don't get any problem in making your logic.Also
[df setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
this format using 12 hour format (means 2:03 pm and 2:03 am) and date object never use am and pm for showing date object value but when you convert it correctly then it gives you right date and time.
If you feel you get any problem then use different locale for that.
It is displaying asper the GMT+4.30 time.It displays like that only.When you are converting that date to string using the DateFormatter it gives the same date(Whichever you want like start date as 01/05/2010 10:15:33 and end date as 01/05/2010 10:45:33).
NSDateFormatter * dateformatter= [[NSDateFormatter alloc]init];
[dateformatter setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
NSString *dat = [dateformatter stringfromDate:My_StartDate];
then you will get the output as 01/05/2010 10:15:33
You might want to set the time zone of the date formatter to GMT here. Do it using
[df setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
before you do dateFromString: calls. This will give you what you want.
Just need to update here in your code:
I might be like that your time would be in 24 hours format, so at that time you need to use this ....other than that you need to set the timezone.
Follow this link for All zone : http://unicode.org/reports/tr35/tr35-6.html#Date%5FFormat%5FPatterns
[df setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
to
[df setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
You are Done ;)