I am new to the concept of TimeZone in iphone.I have struck with small doubt,how can we get all the timezones in the iphone device in the format of (GMT +5:30) Bombay,Calcutta,Madras,New Delhi.
I am trying this by using the api [NSTimeZone knownTimeZoneNames] and place it in an array but it shows in different format but i want the above format.can you please help any body if you know this..
Dude use my code as i shown below. It's need some workaround but you will get what you want.
NSArray *temp = [[NSArray alloc]init];
temp = [NSTimeZone knownTimeZoneNames];
for (id tstring in temp)
{
NSString *tstring2 = [[NSString alloc]init];
tstring2 = tstring;
NSDate *now = [NSDate date];
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setTimeStyle:NSDateFormatterLongStyle];
[df setTimeZone:[NSTimeZone timeZoneWithName:tstring2]];
NSString *alltime = [df stringFromDate:now];
NSLog(#"%# %#",tstring2, alltime);
[tstring2 release];
}
>> OutPut
You will get country data but here i post for some of them only
Asia/Katmandu 4:21:21 PM GMT+05:45
Asia/Kolkata 4:06:21 PM GMT+05:30
Asia/Krasnoyarsk 6:36:21 PM GMT
America/Tortola 6:36:21 AM AST
America/Vancouver 3:36:21 AM PDT
America/Whitehorse 3:36:21 AM PDT
Europe/Copenhagen 12:36:21 PM GMT+02:00
Europe/Dublin 11:36:21 AM GMT+01:00
Europe/Gibraltar 12:36:21 PM GMT
Hope, this will help you..
Related
I want to display date like Today, 25 Mar 2014 15:18, if the date is today's date.
Also expecting Tomorrow, 26 Mar 2014 15:18, if it is tomorrow's date and Wednesday, 27 Mar 2014 15:18, if it is day after tomorrow's date.
Anyone please help me.
Sample from developer.apple.com
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSLocale *frLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setLocale:frLocale];
[dateFormatter setDoesRelativeDateFormatting:YES];
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:60*60*24*1];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"dateString: %#", dateString);
NSDateFormatter *detailFormatter = [[NSDateFormatter alloc] init];
[detailFormatter setLocale:frLocale];
[detailFormatter setDateFormat:#"dd MMM yyyy HH:mm"];
NSString *detailString = [detailFormatter stringFromDate:date];
NSLog(#"detailString: %#", detailString);
NSString *finalString = [NSString stringWithFormat:#"%#, %#",dateString, detailString];
NSLog(finalString);
Apple doesn't directly support using relative date combined with absolute date. What you'd have to do is to create two formatters, one with relative enabled and one without. Then compare the output strings (e.g. "Today" and "Mar 25") and if they are not equal, combine them: "Today, Mar 25", but if they are equal, just use one.
And as an aside, actually on "après-après-demain", I think you may have found a bug (either in docs or code). Do you actually set "après-après-demain"? I tried Apple's sample code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSLocale *frLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"fr_FR"];
[dateFormatter setLocale:frLocale];
[dateFormatter setDoesRelativeDateFormatting:YES];
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:60*60*24*3];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"dateString: %#", dateString);
Which Apple says produces output of dateString: après-après-demain no matter what your language is set to (as it switches the date formatter to French).
But when I ran the code on both iOS and OS X, I get the actual date: Mar 28 (and if I change to *2, I get après-demain.
So, if you're seeing the same, I'd suggest filing a bug report with Apple.
Suppose Twitter returns Wed Sep 14 18:52:57 +0000 2011in JSON format. How do i go about parsing it so that the display looks something like Sep 2011.
Im aware of DateFormatter. I tried the following code but it keeps returning me Null.
NSString *created = [(NSDictionary *)TWData objectForKey:#"created_at"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM yyyy"];
NSDate *date = [dateFormatter dateFromString:created];
tweetingSince.text= [NSString stringWithFormat:#"#%#",[dateFormatter stringFromDate:date]];
You are using same date format to parse the input string created to NSDate and to create final output format from the parsed date. Date formatter is unable to parse date Wed Sep 14 18:52:57 +0000 2011 using date format MMM yyyy. This is why the date is nil.
Edit:
Also quick googling gave me this result on SO: iPhone + Twitter API: Converting time?
Edit 2: Your code with proper NSDate parsing would look like this
NSString *created = [(NSDictionary *)TWData objectForKey:#"created_at"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
// Parse input string to NSDate
[dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss Z yyyy"];
NSDate *date = [dateFormatter dateFromString:created];
// Convert parsed date to output format
[dateFormatter setDateFormat:#"MMM yyyy"];
tweetingSince.text= [NSString stringWithFormat:#"#%#",[dateFormatter stringFromDate:date]];
You can do it like this
NSString *myString = #"Wed Sep 14 18:52:57 +0000 2011";
NSCharacterSet *delimiters = [NSCharacterSet characterSetWithCharactersInString:#" "];
NSArray *components = [myString componentsSeparatedByCharactersInSet:delimiters];
NSLog(#"dilip-%#",components);
Output will be
dilip-(
Wed,
Sep,
14,
"18:52:57",
"+0000",
2011
)
Now you can select any value from array and create new string using that value.
NSString * firstStr = [components objectAtIndex:1];
NSString * secondStr = [components objectAtIndex:5];
NSString * fullStr = [NSString stringWithFormat:#"%# %#",firstStr,secondStr];
NSLog(#"dilip - %#",fullStr);
Output will be
dilip - Sep 2011
How do I convert GMT to IST in IOS ? I have code which will give me time in GMT. But I want to convert it into the corresponding local time zone and use it in my apps. How can I do this ?
Thanks in Advance.
The code below convert GMT to IST.
NSString *inDateStr = #"2000/01/02 03:04:05";
NSString *s = #"yyyy/MM/dd HH:mm:ss";
// about input date(GMT)
NSDateFormatter *inDateFormatter = [[NSDateFormatter alloc] init];
inDateFormatter.dateFormat = s;
inDateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSDate *inDate = [inDateFormatter dateFromString:inDateStr];
// about output date(IST)
NSDateFormatter *outDateFormatter = [[NSDateFormatter alloc] init];
outDateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"IST"];
outDateFormatter.dateFormat = s;
NSString *outDateStr = [outDateFormatter stringFromDate:inDate];
// final output
NSLog(#"[in]%# -> [out]%#", inDateStr, outDateStr);
[(NSDateFormatter *) setTimeZone:[NSTimeZone localTimeZone]]; this will give u the local conversion of the GMT to local time zone.
I was used below code to convert GMT to IST.
NSTimeZone *zone1=[NSTimeZone localTimeZone];
long second=[zone1 secondsFromGMT]; //offset
NSDate *currentdate=[NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMddyyyy hh:mm:ss"]; // format
[dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:second]];
dateStr = [dateFormat stringFromDate:currentdate];
NSLog(#"Current date %#",dateStr);
It may be helpful.
Thank you
Here is the code from the nsdate formatter... for some reason the value dateSelected is incorrect... instead of "April 30 2011 7:55PM" it returns 2011-05-01 02:55... any idea what am i doing wrong?
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"h:mm a"];
objEventInsert.eventtime = [outputFormatter stringFromDate:self.datePicker.date];
NSLog(#"%#",objEventInsert.eventtime);
NSDateFormatter *dateForm = [[NSDateFormatter alloc] init];
[dateForm setDateFormat:#"LLLL d y h:mm a"];
NSDate *dateSelected = [dateForm dateFromString:[NSString stringWithFormat:#"%# %#",objEventInsert.eventstartdate,objEventInsert.eventtime]];
NSLog(#"%#",objEventInsert.eventstartdate);
objEventInsert.date = dateSelected;
NSLog(#"%#",objEventInsert.date);
NSLog response...
2011-04-30 19:54:14.264 APP[24017:207] 7:55 PM
2011-04-30 19:54:16.216 APP[24017:207] April 30 2011
2011-04-30 19:54:17.654 APP[24017:207] 2011-05-01 02:55:00 +0000
That's the correct UTC time. You'll need to set the locale/timezone to get the local time, i.e. 7:55.
See these answer examples
Inconsistent behaviour with NSDateFormatter on two different devices
NSDate dateFromString, how to parse 'around' UTC, GMT and User locale?
Your problem is that you create a new NSDate again and you just initiate it through a string. So either your should create a string in your last step or your need to reuse the NSDateFormatter.
NSString *dateSelected = [NSString stringWithFormat:#"%# %#",objEventInsert.eventstartdate,objEventInsert.eventtime];
NSLog(#"%#", dateSelected);
Note: can use appendStringByFormat to make your code less verbose.
The date you get back from twitter is in this format Fri Aug 07 12:40:04 +0000 2009. I am able to assign the value to a NSDate without issue. However, when I attempt to use NSDateFormatter, I get a nil returned to me. What am I missing?
NSDate *createdAt = [messageData objectForKey:#"created_at"];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"M/d/yy HH:mm"];
NSString *dateString = [format stringFromDate:createdAt];
label.text = dateString;
i had the same question, and i could not resolve it with the current above answers. so here is what worked for me:
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
//Wed Dec 01 17:08:03 +0000 2010
[df setDateFormat:#"eee, dd MMM yyyy HH:mm:ss ZZZZ"];
NSDate *date = [df dateFromString:[[tweets objectAtIndex: storyIndex] objectForKey: TWITTER_CREATED_AT_JSON_KEY]];
[df setDateFormat:#"eee MMM dd yyyy"];
NSString *dateStr = [df stringFromDate:date];
where tweets is an NSMutableArray filled with NSDictionary objects, storyIndex being the row int value (in the tableview), and TWITTER_CREATED_AT_JSON_KEY being a constant NSString with value created_at. use the dateStr wherever you wish to display the date
If the object associated with the #"created_at" key is a valid NSDate object, this code should work.
However, I'm guessing that it is actually an NSString. If so, it will produce the behavior you're describing.
If I'm right, the code snippet above is assigning an NSString object to an NSDate reference. NSDictionary returns untyped 'id' objects, so the compiler won't give you a type mismatch warning.
You'll have to use NSDateFormatter to parse the string into an NSDate (see dateFromString:).
It looks likes, Twitter changes the format again and again. These works for me now. Don't forget to set the locale.
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
//Mon, 29 Oct 2012 12:24:50 +0000
[df setDateFormat:#"EEE, dd LLL yyyy HH:mm:ss ZZZ"];
[df setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]];
NSDate *date = [df dateFromString:[element valueForKey:#"created_at"]];
[df setDateFormat:#"yyyy-MM-dd HH:mm"];
NSString *dateStr = [df stringFromDate:date];
[element valueForKey:#"created_at"] - date from twitter
Above nothing worked for me - try this for Twitter: (I've also included a nice nsdate category for better showing date
lbl = (UILabel *)[cell viewWithTag:16];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
//"created_at": "Tue Jul 09 07:45:01 +0000 2013",
[df setDateFormat:#"EEE MMM d HH:mm:ss ZZZZ yyyy"];
NSTimeInterval timeIntveral = [[df dateFromString:[[_twitterArray objectAtIndex:indexPath.row] objectForKey:#"created_at"]] timeIntervalSince1970];
NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:timeIntveral];
//will work if you added the nsdate category, linked above
NSString *ago = [date timeAgo];
NSLog(#"Output is: \"%#\" %.f", ago,timeIntveral);
lbl.text = ago;
Hope this helps!
First off, what are you using stringFromDate: for? That's if you already have an NSDate and want to make a string representing it.
Moreover, when you do use the date formatter, you're giving it a format string that doesn't match the date string you're trying to interpret.
Change the format string to match your date strings, and use dateFromString: instead of stringFromDate: (with the attendant changes to your variable declarations), and it should work.