Get the time and date of selected time zone? - objective-c

I tried below snippet I'm getting current date and time of system instead of the selected time zone code.
NSDate *now = [NSDate date];
NSCalendar *gregorian=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone timeZoneWithName:selectedzone]];
NSDateComponents* timeZoneComps = [gregorian components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSTimeZoneCalendarUnit fromDate:now];
NSDate *selectedDate=[gregorian dateFromComponents:timeZoneComps];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterFullStyle];
[formatter setDateFormat:#"dd-MM-yyyy HH:mm:ss "];
NSString *strSelectedDate= [formatter stringFromDate:selectedDate];
setZone.text = strSelectedDate;`
Any ideas?

[NSDate date]returns a date object representing the current date and time, no matter where you are. NSDates are not subject to places or time zones. There is just one NSDate that represents now or any other moment for that matter, not different date objects for every time timezone. Therefore, you should not attempt to convert a date between time zones.
NSDate objects represent an absolute instant in time. Consider the following example of how two date representations in different time zones (9/9/11 3:54 PM in Paris and 9/9/11 11:54 PM in Sydney) are actually the same date.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Paris"]];
NSDate *aDate = [formatter dateFromString:#"9/9/11 3:54 PM"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"Australia/Sydney"]];
NSDate *anotherDate = [formatter dateFromString:#"9/9/11 11:54 PM"];
NSLog(#"%#",anotherDate);
if ([aDate isEqualToDate:anotherDate]) {
NSLog(#"How about that?");
}
When it comes to output a date, bear in mind that NSDate's description method returns time in GMT and you need to use a NSDateFormatter to create a date string representing the local time in Paris, Sydney, etc. from a date:
NSDate *now = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"Australia/Sydney"]];
NSLog(#"%#",[formatter stringFromDate:now]); //--> 9/9/11 11:54 PM
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Paris"]];
NSLog(#"%#",[formatter stringFromDate:now]); //--> 9/9/11 3:54 PM

[dateFormatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss z"];
Remove the trailing 'z' character from the format string if you don't want to display the time zone.
EDIT
On the other hand, if you just want to display the timezone name, just make the 'z' uppercase.
EDIT
Lowercase 'z' works fine for all the other timezones, but unfortunately GMT is a special case. So the easiest thing to do is to just omit the 'z' and append " GMT" to the formatted date.

Related

How to get time with date string

How can i get the time from a datetime string according to my time zone. My time zone time is +5:30 GMT.The datetime looks like-
04/03/2013 3:30:00 AM
I want the output like-
9:00:00 AM
Thanks in advance.
You need two date formatters.
One to convert string to date. And then date to string to get required in time format.
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate *date=[dateFormatter dateFromString:#"04/03/2013 3:30:00 AM"];
NSDateFormatter *dfTime = [NSDateFormatter new];
[dfTime setDateFormat:#"hh:mm:ss a"];
NSString *time=[dfTime stringFromDate:date];
*Not compiled and check
If you have date and time in string which is date time in GMT then you need to add GMT in your string. By this you can get actual time of your time zone -
NSString *dateStr = #"04/03/2013 3:30:00 AM GMT";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a ZZZ"];
NSDate *date = [dateFormatter dateFromString:dateStr];
[dateFormatter setDateFormat:#"hh:mm:ss a"];
NSString *opDateStr = [dateFormatter stringFromDate:date];
NSLog(#"op = %#",opDateStr);
NSString *strdate= #"04/03/2013 3:30:00 AM";
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *date=[dateFormatter dateFromString:strdate];
NSDateFormatter *dfTime = [NSDateFormatter new];
[dfTime setDateFormat:#"hh:mm:ss a"];
NSString *time=[dfTime stringFromDate:date];
NSLog(#"%#",time);
NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"dd/MM/yyyy hh:mm:ss a"];
NSLog(#"%#",[formatter stringFromDate:[NSDate date]]);
Output
06/03/2013 10:41:18 AM
[formatter setDateFormat:#"hh:mm:ss a"];
NSLog(#"%#",[formatter stringFromDate:[NSDate date]]);
Output
10:41:18 AM
instead of [NSDate date] pass the required date to be formatted.
As you want the time from the time string you provided according to the current time zone, you need to do something like this
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a zzz"];
NSDate *date=[dateFormatter dateFromString:#"04/03/2013 3:30:00 AM 0000"];
NSDateFormatter *dfTime = [NSDateFormatter new];
[dfTime setTimeZone:[NSTimeZone systemTimeZone]];
[dfTime setDateFormat:#"hh:mm:ss a"];
NSString *time=[dfTime stringFromDate:date];
NSLog(#"%#",time);
you wont get the real output unless there is the offset present which is the 0000.(its the GMT offset). Also the time formatter wont work perfectly if the format of the dateString and its formatter are different. So you need to do it MM/dd/yyyy hh:mm:ss a zzz, zzz is the offset. Now your output will be 09:00:00 AM

Convert date time to NSDate from NSString

Time zone always made trouble for me. This time I have to convert date-time , that I have in NSSTring to NSDate.
I am doing something like this.
NSString *myStringDate=#"14-11-2012 4:09:00 PM +0500"
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
[formatter setDateFormat:#"dd-MM-yyyy h:mm:ss a Z"];
NSDate *aDate = [formatter dateFromString:myStringDate];
NSLog(#"%#",aDate);
but I am having the output like this
14-11-2012 11:09:00 +0000
Also, no AM/PM is setting :-(
What I want is 14-11-2012 4:09:00 PM +0500 i.e same Date-Time that I have in string.
Thanks in anticipation.
the output of your converting is giving the GMT time
NSString *myStringDate=#"14-11-2012 4:09:00 PM +0500"
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
[formatter setDateFormat:#"dd-MM-yyyy h:mm:ss a Z"];
NSDate *aDate = [formatter dateFromString:myStringDate];
NSDateFormatter* df_utc = [[[NSDateFormatter alloc] init] autorelease];
[df_utc setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[df_utc setDateFormat:#"yyyy.MM.dd G 'at' HH:mm:ss zzz"];
NSDateFormatter* df_local = [[[NSDateFormatter alloc] init] autorelease];
[df_local setTimeZone:[NSTimeZone timeZoneWithName:#"EST"]];
[df_local setDateFormat:#"yyyy.MM.dd G 'at' HH:mm:ss zzz"];
NSString* ts_utc_string = [df_utc stringFromDate:aDate];
NSString* ts_local_string = [df_local stringFromDate:aDate];
Here Is the Change need to be taken.
[formatter setDateFormat:#"dd-MM-yyyy h:mm:ss a "];
Thanks
The conversion looks correct, but the default output is in UTC time (+0000) and with a 24h clock which is why you don't get the AM/PM distinction (“01:00 PM” would be 13:00). You also need to format the output if you want a specific representation ([formatter stringFromDate:aDate])…
For the UTC issue you need to select the time zone of your choice, by giving that choice in place of [NSTimeZone systemTimeZone]. Try [NSTimeZone localTimeZone] for starters.

how to display time and date in a label of selected zone xcode

Can any 1 help me to get this problem done I'm trying to display date and time of selected time zone n a label or text field I tried below code but its Displaying current time of system for all selected zone :( thanks in advance
NSDate *now = [NSDate date];
NSCalendar *gregorian=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone timeZoneWithName:selectedzone]];
NSDateComponents* timeZoneComps = [gregorian components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSTimeZoneCalendarUnit fromDate:now];
NSDate *selectedDate=[gregorian dateFromComponents:timeZoneComps];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterFullStyle];
[formatter setDateFormat:#"dd-MM-yyyy HH:mm:ss "];
NSString *strSelectedDate= [formatter stringFromDate:selectedDate];
zone.text = strSelectedDate;
[NSDate date]returns a date object representing the current date and time, no matter where you are. NSDates are not subject to places or time zones. There is just one NSDate that represents now or any other moment for that matter, not different date objects for every time timezone. Therefore, you should not attempt to convert a date between time zones.
NSDate *now = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"Australia/Sydney"]];
NSLog(#"%#",[formatter stringFromDate:now]); //--> 9/9/11 11:54 PM
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Paris"]];
NSLog(#"%#",[formatter stringFromDate:now]);

How to get date and time of selected time zone from uipicker in Xcode

I'm trying to display date and time of selected time zone from uipicker in a label or text field I'm not getting any way to solve this problem, so please any one help me Thanks in advance.
SelectedTimeZone= [Weight objectAtIndex:[pickerView selectedRowInComponent:0]];
NSString *selectedzone = SelectedTimeZone;
NSCalendar *gregorian=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone timeZoneWithName:selectedzone]];
NSDateComponents *timeZoneComps=[[NSDateComponents alloc] init];
NSDate *date=[gregorian dateFromComponents:timeZoneComps];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterFullStyle];
[formatter setDateFormat:#"dd-MM-yyyy HH:mm:ss "];
NSString *todaysDate= [formatter stringFromDate:[NSDate date]];
zone.text = todaysDate; I know this is not proper way but i'm new to xcode
[NSDate date]returns a date object representing the current date and time, no matter where you are. NSDates are not subject to places or time zones. There is just one NSDate that represents now or any other moment for that matter, not different date objects for every time timezone. Therefore, you should not attempt to convert a date between time zones.
NSDate *now = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"Australia/Sydney"]];
NSLog(#"%#",[formatter stringFromDate:now]); //--> 9/9/11 11:54 PM
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Paris"]];
NSLog(#"%#",[formatter stringFromDate:now]);
Or Refer same question here
To get date and time of selected time zone from uipicker provide date to formatter like this:
NSDate *now = [NSDate date];
NSCalendar *gregorian=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone timeZoneWithName:selectedzone]];
NSDateComponents* timeZoneComps = [gregorian components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSTimeZoneCalendarUnit fromDate:now];
NSDate *selectedDate=[gregorian dateFromComponents:timeZoneComps];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterFullStyle];
[formatter setDateFormat:#"dd-MM-yyyy HH:mm:ss "];
NSString *strSelectedDate= [formatter stringFromDate:selectedDate];
zone.text = strSelectedDate;

NSDate date method returns wrong result

I know there is a lot of questions of this type, but I did't find solution for my case;
I need to get current and correct NSDate object, not NSString!
This code returns wrong time (+3 hours), because I'm from Ukraine.
NSDate *currentDate = [NSDate date];
How to get current NSDate object from NSDateFormatter? I know only how to get NSString, but I don't need it.
EDIT: I'm using this code to compare 2 NSDates using NSCalendar object, here is code:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *components = [calendar components:unitFlags fromDate:nowDate toDate:endDate options:0];
and components.hour shows me +3 hours difference
NSDates are always stored in UTC, actually the dates itselfs dont know anything about timezones or weeks, month, years. They are just a point in time.
To see the correct time for your position on the earth surface, you need to take the NSCalendar, that represents your time model in account. You could use it directly and mess around with your dates, or create a NSDateFormatter that will leave the dates untouched but adjust their appearence to your needs.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterFullStyle];
[formatter setTimeStyle:NSDateFormatterFullStyle];
NSLog(#"%#", [formatter stringFromDate: date1]);
results in
Thursday, July 12, 2012, 4:36:07 PM Central European Summer Time
in response to the comment:
try this code as test
NSDate *now = [NSDate date];
NSDate *startOfToday = nil;
NSDate *startOfThisWeek = nil;
NSDate *startOfThisMonth = nil;
NSDate *startOfThisYear = nil;
[[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit startDate:&startOfToday interval:NULL forDate:now];
[[NSCalendar currentCalendar] rangeOfUnit:NSWeekCalendarUnit startDate:&startOfThisWeek interval:NULL forDate:now];
[[NSCalendar currentCalendar] rangeOfUnit:NSMonthCalendarUnit startDate:&startOfThisMonth interval:NULL forDate:now];
[[NSCalendar currentCalendar] rangeOfUnit:NSYearCalendarUnit startDate:&startOfThisYear interval:NULL forDate:now];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterFullStyle];
[formatter setTimeStyle:NSDateFormatterFullStyle];
NSLog(#"%#", now);
NSLog(#"%#", [formatter stringFromDate:now]);
NSLog(#"%#", startOfToday);
NSLog(#"%#", [formatter stringFromDate:startOfToday]);
NSLog(#"%#", startOfThisWeek);
NSLog(#"%#", [formatter stringFromDate:startOfThisWeek]);
NSLog(#"%#", startOfThisMonth);
NSLog(#"%#", [formatter stringFromDate:startOfThisMonth]);
NSLog(#"%#", startOfThisYear);
NSLog(#"%#", [formatter stringFromDate:startOfThisYear]);
you will realize, that the start of the day, week, month and year will be adjusted to your local time, as the first of each NSLog-pair will give you the date in UTC and the second in your local time zone.
on the chat you posted this code
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
[dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *date = [dateFormat dateFromString:dateString];
[dateFormat release];
so the problem is, that the datestring is actually not from GMT, but EET (Eastern European Time)
try
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"EET"]];
but the by far most elegant solution would be to get the datestring with the offset to UTC, similar to 2012-07-12 12:23:00 +0300if some how possible.
In such an case you could parse it with
[dateFormat setDateFormat:#"yyyy-mm-dd HH:mm:ss Z"];
and don't need further time zone handling, as the formatter knows the offset via the Z-specifier.
Also note, that if you don't set any timezone, the device's current should be used. If the user is always in the same timezone as the time from the date string, this should work, too. But if the user leaves that zone, or you inted to have it working world wide, you should use one of the solutions I gave you. With the second (specifying the timezone with-in the datestring) as the preferred one.
The date probably has the correct value; perhaps you think it's incorrect because it looks wrong when you log it? This is because when you log it, it shows the date in UTC. You can get the string in your local timezone by using an NSDateFormatter.
The answer is correct if the second date is in UTC. The Ukraine is currently 3 hours ahead of UTC so midnight UTC is at 21:00 in the Ukraine. 21:00 - 18:00 is 3 hours. So check how you are obtaining the second date. The wrong time zone is probably being specified for it.
If the first date really was created at 18:00 Ukraine time via
nowDate = [NSDate date];
then the second date really is 21:00 Ukraine time which corresponds to midnight UTC. Since you claim it is midnight, you must have used UTC to create it from a string - either implicitly or explicitly. Show us how you created it.
** EDIT **
The code you gave me on chat is this:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
[dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *date = [dateFormat dateFromString:dateString];
[dateFormat release];
The third line sets the time zone to GMT which is identical to UTC for all intents and purposes. Your date formatter is probably initialised to the correct locale for the device and so probably you can just leave that line out.