retaining the time zone in NSDateFormatter - cocoa

I have to set a format string for a DateFormatter to convert a NSString to a NSDate.
The string is in the format: 2011-01-31 12:45:00 +0200 (y-m-d h:m:s timezone)
I'm using the format: #"yyyy'-'MM'-'dd' 'HH':'mm':'ss' 'Z" , but the timezone is converted to +0000 and the hour is adjusted, so a string like:
2011-01-31 12:45:00 +0200 is converted to:
2011-01-31 10:45:00 +0000
The Code is here:
- (NSDate *) dateForString: (NSString *)dateString
{
NSDateFormatter *dateFormatter;
NSLocale *enUSPOSIXLocale;
NSDate *date;
dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
enUSPOSIXLocale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease];
[dateFormatter setLocale:enUSPOSIXLocale];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd' 'HH':'mm':'ss' 'Z"];
// [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
date = [dateFormatter dateFromString:dateString];
return date;
}

This is absolutely expected behaviour when using NSDate - NSDate represents a moment in time so in your case the 2 date/times are equivalent.
Just make sure when you display the date using the a dateFormatter you set the timezone on the date formatter to how you want it displayed.

Have a look at setTimeZone: in
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
Some more info can also be found here:
NSDate - Convert Date to GMT (maybe duplicate?)

Related

How to get current time along with the Timezone in XCode?

I tried this :
NSDate *date = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd hh:mm:ss TZD"];
NSString *dateString = [dateFormat stringFromDate:date];
NSLog(#"Current Date : %#",dateString);
and the result I get is Date and Time but not the Timezone.
#Vaibhav you have to use time zone like this
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
refer this link
Get the time and date of selected time zone?

Converting string from xml to NSDate and how to translate the date

This is my data string:
<pubDate>Sun, 24 Mar 2013 00:42:13 +0200</pubDate>
I want to convert this to "Sun, 24/3/13, 00:42"
and to translate the day name to hebrew.
here is my code so far:
currentPubDate = [self.xmlParser.pubDate objectAtIndex:indexPath.row];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSLocale *loacle = [[NSLocale alloc]initWithLocaleIdentifier:#"he_IL"];
[dateFormatter setLocale:loacle];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
[dateFormatter dateFromString:currentPubDate];
What should I do to make it work? I've read few guides but without success.
Here is the answer for your first part
NSString *currentPubDate = #"Sun, 24 Mar 2013 00:42:13 +0200";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:currentPubDate];
[dateFormatter setDateFormat:#"EEE, dd/M/yy HH:mm"];
NSLog(#"%#", [dateFormatter stringFromDate:date]);
For the translation part you have to set locale, but thats not working in my system as well.

Google Xml parser

can anyone help me in getting the formatted date from the parsed XML.
I can format that using NSXML formatter but now i am found difficulty in using it through Google XMl parser. where i can get the values through the key but i need that in a formatted way,
return [NSDictionary dictionaryWithObjectsAndKeys:
[[[xmlItem elementsForName:#"title"] objectAtIndex:0] stringValue], #"title",
[[[xmlItem elementsForName:#"link"] objectAtIndex:0] stringValue], #"link",
[[[xmlItem elementsForName:#"pubDate"] objectAtIndex:0] stringValue], #"Date",
nil];
here the pubDate is returned as such from xml with hr,min,sec i need the date alone.
Thanks in advance!!!!
I got the answer
//cell.textLabel.text = [item objectForKey:#"pubDate"];
NSString *yourXMLDate = [item objectForKey:#"pubDate"];
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"E, d LLL yyyy HH:mm:ss Z"];
NSDate *inputDate = [inputFormatter dateFromString:yourXMLDate];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"MMM dd, HH:mm a"];
NSString *outputDate = [outputFormatter stringFromDate:inputDate];
cell.textLabel.text=outputDate;
where this results in Oct 12, 12:30 AM. whatever parser used in the xcode this works fine....

NSDictionary object to NSDate

I need help about this issue.
I would convert a NSString object got by key from a NSMutableDictionary into a NSDate
This is the object from the dictionary
[elemento objectForKey:#"data"]
Here is the entire code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// Time format for the string value
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss +0000"];
NSDate *date = [dateFormatter dateFromString:[elemento objectForKey:#"data"]];
NSLog(#"date => %#", date);
[dateFormatter setDateFormat:#"d MMM yyyy HH:mm:ss Z"];
NSString *dateStr = [dateFormatter stringFromDate:date];
NSLog(#"date modded => %#", dateStr);
`
But from the console I obtain this messages:
2012-08-27 11:51:31.961 iRSSReader[2319:c07] date from dictionary => Thu, 03 May 2012 16:55:02 +0000
2012-08-27 11:51:31.962 iRSSReader[2319:c07] date => (null)
2012-08-27 11:51:31.962 iRSSReader[2319:c07] date modded => (null)
If I try with a NSString value, instead of NSDictionary value, for example
NSString *dateStr = #"Thu, 03 May 2012 16:55:02 +0000"
it works fine.
Thanks in advance
Possible elemento not contains key "data" -> [elemento objectForKey:#"data"] is nil

Time Zone conversion form stringdate

I have a stringdate 16-MAY-2010 23:04:44 which i need to convert to gmt time zone that is the out put required is 17-May-2010 12:03:03.
I used date formatters to convert but the result i am getting is not in the format i required.I am sending the code please let me know if i am doing correct or not
Here is the code:
NSString *timeStamp = [format stringFromDate:[NSDate date]];
NSString *output = [timeConv dateStringFromString:timeStamp];
- (NSString *)dateStringFromString:(NSString *)sourceString
{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setDateFormat:#"dd-MMMM-yyyy HH:MM:ss"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSDate *date = [dateFormatter dateFromString:sourceString];
return [dateFormatter stringFromDate:date];
}
The out put i am getting is the same old time without conversion. so please let me know the correct solution.
I know no cocoa whatsoever, but if I'm understanding your code correctly, you're never telling the parser that your original string is not GMT, but rather GMT-1.
Based on your own code (and again, with no knowledge of cocoa), this is what I believe you need to do:
NSTimeZone *gmtminusone = [NSTimeZone timeZoneForSecondsFromGMT:-3600.0];
[dateFormatter setTimeZone:gmtminusone];
NSDate *date = [dateFormatter dateFromString:sourceString];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
return [dateFormatter stringFromDate:date];
If it doesn't work right away, maybe it gives you the idea of what to do:
Parse the string with a date formatter set to GMT-1, to output a date.
Parse the date with a date formatter set to GMT, to output a string.
The date string didn't include a time zone specification, so the date formatter relied on you to tell it what time zone it's in.
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSDate *date = [dateFormatter dateFromString:sourceString];
You told it that the input string was in GMT.
You need to tell the date formatter what time zone the date is in. Once you've done that, you can tell the formatter to convert the string to a date (dateFromString:), and it will interpret the string as representing a date in that time zone.
Then, once you have the date so interpreted, switch the formatter's time zone to GMT and have it output the string (stringFromDate:). That string will represent the date converted to GMT.

Resources