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
Related
This question already has answers here:
Convert string to date in my iPhone app
(4 answers)
issue with date formatting using NSDateFormatter
(3 answers)
Closed 9 years ago.
How can i convert the following string "October 24,Monday 12:30 AM EST" to NSDateFormat.
i tried this code
NSDateFormatter *dateformat=[[NSDateFormatter alloc]init];
[dateformat setDateFormat:#"MM/DD/YYYY hh:mm a"];
NSDate *datefor=[dateformat dateFromString:appDelegate.appoinmentString];
[dateformat setDateFormat:#"MM-dd-yyyy hh:mm a"];
NSString *dateStr=[dateformat stringFromDate:datefor];
NSDate *datetype=[dateformat dateFromString:dateStr];
Your dateformat for October 24,Monday 12:30 AM EST is not correct. The correct dateformat in your case is MMMM dd,eeee HH:mm a z.
Working Code :
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:#"MMMM dd,eeee HH:mm a z"];
NSDate *myDate = [dateformat dateFromString:#"October 24,Monday 12:30 AM EST"];
Take a look at Date Format Specifiers.
eeee - Local day of week spelled out.
MMMM - Month spelled out.
dd - day of month with no leading zeros.
HH - hour of day (24 hour format).
mm - minutes of hour (with leading zero) .
z - timezone (short wall
time).
Try this...
NSString *p=#"October 24,Monday 12:30 AM EST";
NSDateFormatter *dateformat=[[NSDateFormatter alloc]init];
[dateformat setDateFormat:#"MMM dd,EEEE hh:mm a vvv"];
NSDate *datefor=[dateformat dateFromString:p];
[dateformat setDateFormat:#"MM-dd-yyyy hh:mm a"];
NSString *dateStr=[dateformat stringFromDate:datefor];
NSDate *datetype=[dateformat dateFromString:dateStr];
Let me know if you have any problem.
Try like below:-
NSString *dateStr=#"October 24,Monday 12:30 AM EST";
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"EST"]];
[dateFormat setDateFormat:#"MMMM dd,eeee HH:mm a z"];
NSDate *changeDate=[dateFormat dateFromString:dateStr];
NSLog(#"changeDate=%#",changeDate);
NSString *str=[dateFormat stringFromDate:changeDate];
//getting your local time
NSTimeZone *tz=[NSTimeZone localTimeZone];
//setting yourlocal time
[dateFormat setTimeZone:tz];
NSDate *date = [dateFormat dateFromString:str];
//Setting your desired format
[dateFormat setDateFormat:#"dd-mm Z"];
NSString *newDate=[dateFormat stringFromDate:date];
NSLog(#"new date=%#",newDate);
One simple thing on conversion from NSString to NSDate. How can I convert Mon, 27 August 2012 01:30 AM to NSDate in this same format. I tried with NSDateFormatter. But I am not getting it in this required format. Can anyone help? This is what I tried.
NSDateFormatter *df=[[NSDateFormatter alloc] init];
[df setDateFormat:#"EEEE,dd MM yyyy HH:mm a"];
NSDate *date1 = [df dateFromString:#"Mon,27 August 2012 01:30 AM"];
NSLog(#"%#",date1);
NSDateFormatter is to specify the format that will appear in the date-string when extracting string from date or the format that is in the date-string when extracting date from string
So whenever you extract NSDate from a NSString, NSDate is always obtained in default date format(eg 2012-08-27 00:30:00 +0000)... only the when you extract NSString from NSDate, NSString can be obtained in desired(custom) format that you set in NSDateFormatter.
I hope this will help you sure!
NSDateFormatter *dateformater=[[[NSDateFormatter alloc] init]autorelease];
[dateformater setDateFormat:#"EEEE,dd MMMM yyyy HH:mm a"];
NSDate *todayTmp=[NSDate date];
NSString *conversionDate=[dateformater stringFromDate:todayTmp];
Note : (Upper case) HH for 24h time format, (Lower case) hh for 12h time format
NSString *myDateAsAStringValue = #"Mon, 27 August 2012 01:30 AM";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE, dd MMM yyyy HH:mm a"];
NSDate *myDate = [[NSDate alloc]init];
myDate = [df dateFromString:myDateAsAStringValue];
[df release];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm a"];
NSString *strDate = [dateFormatter stringFromDate:myDate];
NSLog(#"%#", strDate);
[dateFormatter release];
please use below code
NSDateFormatter *df=[[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE,dd MMMM yyyy hh:mm a"];
NSDate *date1 = [df dateFromString:#"Mon,27 August 2012 01:30 AM"];
NSLog(#"%#",date1);
your formatter is wrong
check this one
NSLog will return NSDate in a fixed format, i guess.
If we need Date in different format, we should have to format it via NSDateFormatter and get it as NSString.
Just a guess.
Don't forget to set the correct locale! If your device does not use an english locale NSDateFormatter can have problems to convert Mon and August into useful information because Mon is not the correct abbreviation for Monday in your language. For example in Germany the correct three letter abbreviation for Monday is Mon..
If you parse dates that have words in it you have to set the correct locale.
This should work:
NSDateFormatter *df=[[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE,dd MMMM yyyy hh:mm a"];
NSLocale *posixLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[df setLocale:posixLocale];
NSDate *date1 = [df dateFromString:#"Mon,27 August 2012 01:30 AM"];
NSLog(#"%#",date1);
EEE is the dateformatter code for a three letter weekday abbreviation.
hh is the dateformatter code for Hours between 1 and 12. HH means 0-23
MMMM is the full month, MM would be the numeric value (= 08) of the month.
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 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 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.