I have a string that I want to parse the time from:
NSString *longdate = #"Mar 27, 2011 8:38:38 PM";
I want to parse this date and output just the time portion w/ hours+minutes+am/pm:
// First, convert our string into an NSDate
NSDateFormatter *inFormat = [[NSDateFormatter alloc] init];
[inFormat setDateFormat:#"MMM dd, yyyy HH:mm:ss aaa"];
NSDate *date = [inFormat dateFromString:longdate];
[inFormat release];
// Now convert from date back to a string
NSDateFormatter *outFormat = [[NSDateFormatter alloc] init];
[outFormat setDateFormat:#"HH:mm aaa"];
NSString *final = [outFormat stringFromDate:date];
[outFormat release];
NSLog(#"original: %# | final %#", longdate, final);
The problem is the final time is wrong. I expect the time to be 8:38 PM, but instead I get 12:38 PM.
I just want to get the same hour out that I put it, and not bother w/ any time zones or locales. What am I doing wrong here? Thanks.
Found the problem. Had nothing to do with timezones and everything to do with using the wrong formatting codes for the date formatter.
[inFormat setDateFormat:#"MMM dd, yyyy HH:mm:ss aaa"];
should be:
[inFormat setDateFormat:#"MMM dd, yyyy h:mm:ss aaa"];
Likewise, outFormat's dateformat should be:
[outFormat setDateFormat:#"h:mm aaa"];
After this adjustment everything works fine even w/o any TimeZone adjustments.
As Dave said, check your time zones. You can tell the date formatter to use your current time zone as well:
[outFormat setTimeZone:[NSTimeZone localTimeZone]];
You've got your timezones messed up. IIRC, NSDateFormatter (by default) will parse stuff in the UTC timezone (+000), but dates are NSLogged in your current timezone. Or something like that.
Anyway, check your timezones.
Related
I have two UIDatePicker in a view. One for getting Date and Another for the Time. I am getting the Time properly. Now I need to Append Time with the Date for setting firedate of Local Notification. On appending both, I am getting null in NSDate
NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
[dateFormat setDateFormat:#"MMM,dd YYYY HH:mm a"];
[dateFormat setLocale:[NSLocale currentLocale]];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *tempDate = [dateFormat dateFromString:[NSString stringWithFormat:#"%# %#",txtDate.text,txttime.text]];
localNotif.fireDate = tempDate;
While txtDate.text is Aug,06 2012 and txtTime.text is 7:04 pm.
I am getting tempDate as null. I have tried various date format for this. Please suggest.
YYYY-MM-dd HH:mm:ss doesnt match
Aug,06 2012 7:04 pm
it should be MMM,dd YYYY HH:mm a
also since you're pm is in lower case, you may need to set the AMSymbol and PMSymbol properties of the date formatter
Check with this If helpful.
MMM,dd yyyy HH:mm
I want to make date by date formatter
2012-07-12 but it display like
2012-07-11
My code:
NString * today_selected=[NSString stringWithFormat:#"%d%#%d%#%d",year_for_activated,#"-",month_for_activated,#"-",taged]; NSDateFormatter *Df=[[NSDateFormatter alloc]init];
//here year_of_=2012 and month_of_ac=7, and tag=12
but it display 2012-07-11 instead of 12.
[Df setDateFormat:#"YYYY-MM-DD" ];
NSDate *date_selected=[Df dateFromString: today_selected];
NSLog(#"today_selected:%#",date_selected);
but it display 2012-01-12
Please read the documentation which states
It uses yyyy to specify the year component. A common mistake is to use
YYYY. yyyy specifies the calendar year whereas YYYY specifies the year
(of "Week of Year"), used in the ISO year-week calendar. In most
cases, yyyy and YYYY yield the same number, however they may be
different. Typically you should use the calendar year.
Also you will note that the day is dd, NOT DD
When you find a problem like this, your first stop should be the documentation
try this:
NString *today_selected=#"2012-07-12";
NSDateFormatter *Df=[[NSDateFormatter alloc]init];
[Df setDateFormat:#"yyyy-MM-dd" ];
NSDate *date_selected=[Df dateFromString: today_selected];
NSLog(#"today_selected:%#",date_selected);
try this for get the 2012-07-12 ,this type of Formatter :
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDate *now = [[NSDate alloc] init];
NSString *theDate = [dateFormat stringFromDate:now];
I'm developing an application and I need to parse a Tweet timestamp that actually is a string like this:
"Mon, 17 Oct 2011 10:20:40 +0000"
However what I need it is only the time. So what I'm trying to get it is:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm"];
NSDate *date = [dateFormatter dateFromString:timestamp];
but when I try to print the Date with NSLog the output is (null) and I don't understand why. What I need, if it is possible, is to create a date object only with the time. Indeed later on I need to compare different dates but I care only about the time. It is not important if the date are different because of the day, month or year, the important is that I can compare them with the "timeIntervalSinceDate" to get the difference in seconds.
Any help will be really appreciated.
Thanks
Ale
NSDATE is al full date object, it needs a date en time.
You can use NSDateFormatter to only display the time, but you will need to parse the full string to get the NSDate object.
Here some code you can use:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//Mon, 11 Jul 2011 00:00:00 +0200
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"EN"] autorelease]];
[dateFormatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss ZZZZ"];
NSDate *date = [dateFormatter dateFromString: timestamp];
[dateFormatter release], dateFormatter = nil;
You need to set the local to make sure it read the timestamp in the correct language.
Also alloced the dateFormatter outside any loops and release it after you're done with the loop.
Look at NSDateComponents. You should be able to create a date from components. You'll have to parse the timestamp yourself, though (which should be easy — just split on the colon and grab the number from each component).
I want current date and time in PST. I used this code
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss zzz"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"PST"]];
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"String:%#",timeStamp);
It returns correct date and time in PST in string form but I want NSDate in PST. So when I change NSString to NSDate like this:
NSDate *currentPST = [dateFormatter dateFromString:timeStamp];
NSLog(#"currentPST Date:%#",currentPST);
It returns date in GMT. I have done R&D but all in vain.Output is:
String:2011-05-18 22:28:54 PDT
currentPST Date:2011-05-19 05:28:54 +0000
Can anyone suggest a solution please.
Thanks in advance
In Cocoa, NSDate is an abstract representation of a date with no time zone information applied.
Whenever you print a NSDate object, it will print the date value corresponds to the default timezone(your device timezone). Your device timezone is GMT thats why you get the value like that. If you look into that deeply, both the time where same, but the timezone varies.
I have a very strange date format coming to me via JSON. e.g. - "July, 18 2010 02:22:09"
These dates are always UTC. I'm parsing the date with NSDateFormatter, and setting the timeZone to UTC...
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[inputFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]];
[inputFormatter setDateFormat:#"MMMM, dd yyyy HH:mm:ss"];
NSDate *formatterDate = [inputFormatter dateFromString:dtStr];
However, the date when logged is appearing with the offset of my device...
"2010-07-18 02:22:09 -0600"
What am I doing wrong here?
I think this is because your NSDate isn't using the UTC timezone. The docs say this about the description method of NSDate:
A string representation of the receiver in the international format YYYY-MM-DD HH:MM:SS ±HHMM, where ±HHMM represents the time zone offset in hours and minutes from GMT (for example, “2001-03-24 10:45:32 +0600”).
So, you aren't really doing anything wrong. NSDate is behaving as expected. What are you trying to accomplish? Do you need the date in a string? Are you doing date comparison?