create a NSDate from a string - objective-c

I have an NSString like this: #"3/15/2012 9:15 PM" and I would like to convert it to NSDate, I have done like this:
NSString *str =#"3/15/2012 9:15 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"mm/dd/yyyy HH:mm"];
NSDate *date = [formatter dateFromString:];
NSLog(#"%#", date); // date = null
Can you help me please, thanks.

Use the following solution
NSString *str = #"3/15/2012 9:15 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"MM/dd/yyyy HH:mm a";
NSDate *date = [formatter dateFromString:str];
NSLog(#"%#", date);
Edit:
Sorry, the format should be as follows:
formatter.dateFormat = #"MM/dd/yyyy hh:mm a";
And the time shown will be GMT time. So if you add/subtract the timezone, it would be 9:15 PM.
Edit: #2
Use as below. You would get exact time too.
NSString *str = #"3/15/2012 9:15 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"MM/dd/yyyy hh:mm a";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
formatter.timeZone = gmt;
NSDate *date = [formatter dateFromString:str];
NSLog(#"%#",date);

Your formatter is wrong. According to the NSDateFormatter documentation it should be "MM/dd/yyyy h:mm a". You also probably want to set the locale as stated in the Date Formatting Guideline:
If you're working with fixed-format dates, you should first set the
locale of the date formatter to something appropriate for your fixed
format. In most cases the best locale to choose is en_US_POSIX, a
locale that's specifically designed to yield US English results
regardless of both user and system preferences.
Try this:
NSString *str = #"3/15/2012 9:15 AM";
NSLocale *enUSPOSIXLocale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setLocale:enUSPOSIXLocale];
[formatter setDateFormat:#"MM/dd/yyyy h:mm a"];
NSDate *date = [formatter dateFromString:str];
NSLog(#"%#",date);
Outputs:
2012-03-19 12:37:27.531 Untitled[6554:707] 2012-03-15 08:15:00 +0000

Hmmm - you've gone wrong for a couple of reasons
the NSDateFormatter is strict and you have not been.
you didn't supply str to dateFromString:
NSString* str = #"3/15/2012 9:15 PM";
NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"MM/dd/yyyy HH:mm a"];
NSDate* date = [formatter dateFromString:str];
NSLog(#"%#",date);

NSString *str =#"3/15/2012 9:15 PM";
NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];
[dateformatter setDateFormat:#"MM/dd/yyyy"];
NSArray *firstSplit = [str componentsSeparatedByString:#" "];
NSDate *dateSelected = [dateformatter dateFromString:[firstSplit objectAtIndex:0]];
[dateformatter setDateFormat:#"mm:ss"];
NSDate *dateSelected2 = [dateformatter dateFromString:[firstSplit objectAtIndex:1]];
NSLog(#"%#",[dateformatter stringFromDate:dateSelected2]);
NSLog(#"%#",[dateformatter stringFromDate:dateSelected]);

Related

Format date with NSDateFormatter

I want to format 06-06-2013 1:51 PM as Jun 06,2013.
I have tried all possible different formatting styles using NSDateFormatter but failed.
Try this
First You need to convert this string back to NSDate then again convert the NSdate to string using formatter.
NSDateFormatter *dateFormatForDB = [[NSDateFormatter alloc] init];
[dateFormatForDB setDateFormat:#"dd-MM-yyyy HH:mm a"]; //Note capital H is 4 24-hour time format
NSDate *aDate = [[[NSDate alloc] initWithTimeInterval:0 sinceDate:[dateFormatForDB dateFromString:aDateString]] autorelease];
if(aDate){
[formatter setDateFormat:#"MMM dd,yyyy"];
NSString *date = [formatter stringFromDate:aDate];
[dateFormatForDB release];
}
Try to use this format
[df setDateFormat:#"MMM dd,yyyy];
By looking at your format, i think this is what you are looking for...
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd-MM-yyyy hh:mm a"];
NSDate *newDate = [df dateFromString:dateString];
NSDateFormatter *df2 = [[NSDateFormatter alloc] init];
[df2 setDateFormat:#"MMM dd,yyyy"];
NSString *formattedDate = [df2 stringFromDate:newDate];
This will give you the date as string in the required format. If you want time in 24hr format, replace 'hh' with 'HH'.

NSString to NSDate returns wrong date

I try to convert my NSString to NSDate object, but NSDateFormatter returns me a strange value.
Here is code:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *date = [dateFormat dateFromString:#"2012-08-15 00:00"];
[dateFormat release];
date value is 2012-08-14 21:00 +0000. It is 3 hours difference between NSString value and NSDate value. I think I've missed something, but I don't know what.
This is what i use:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
NSDate *date = [dateFormat dateFromString:#"2012-08-15 00:00:00 +0000"];
NSLog(#"\n\n DATE: %# \n\n\n", date);
The +0000 is timezone, so make sure you use your timezone, like +0400.
Edit:
If you can't change the string, you can use this code to do it:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
[dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *date = [dateFormat dateFromString:#"2012-08-15 00:00"];
As i knew NSDate holds Grinwich time, so if you are in Moscow time zone, everything is wright
In objective c for NSDate if you did not set the setTimeZone, NSDate will take default timezone as localTimeZone. so if you need to get the exact date which you give as NSString string format, you need to setTimeZone as UTC. Follow the sample code, I guess it will be helpful for you.
NSDateFormatter *loacalformatter=[[NSDateFormatter alloc]init];
[loacalformatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *localDate =[loacalformatter dateFromString:#"2012-08-15 00:00"];
NSLog(#"localDate :%#",localDate);
NSDateFormatter *UTCformatter=[[NSDateFormatter alloc]init];
[UTCformatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[UTCformatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *UTCDate =[UTCformatter dateFromString:#"2012-08-15 00:00"];
NSLog(#"UTCDate :%#",UTCDate);
UTCDate :2012-08-15 00:00 +0000 (GMT+00:00)
As suggested in the comments, if the date you receive is UTC then you need to convert it to your local timezone. Apple recommend you always use a properly configured NSDateFormatter when displaying dates, to handle localisation issues.
Here's some example code for turning an NSDate into an NSString:
NSDate *date = // initialised elsewhere
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.locale = [NSLocale currentLocale];
dateFormat.timeZone = [NSTimeZone localTimeZone];
dateFormat.timeStyle = NSDateFormatterShortStyle;
dateFormat.dateStyle = NSDateFormatterShortStyle;
dateFormat.locale = [NSLocale currentLocale];
NSString *dateAsString = [dateFormat stringFromDate:date];

NSDateFormatter and /'s

I have a NSString that I want to load into a NSDate. The string looks like: 21/10/2011 5:42:02 PM
What I have written is:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"d/M/yyyy h:mm:ss a"];
NSDate *date = [dateFormat dateFromString:dateString];
The date ends up with a null value.
There is obviously something wrong with the date formatter but I am not sure what.
Any help would be greatly appreciated.
If the string doesn't match the format, then it will return null.
For example, here's an example that matches the format and one that doesn't:
NSString *dateString = #"11/09/2011 12:22:02";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"d/M/yyyy h:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateString];
NSLog(#"dateString; %#", date);
NSString *dateString2 = #"2011-11-09 12:22:02 +0000";
NSDate *date2 = [dateFormat2 dateFromString:dateString2];
NSLog(#"dateString; %#", date2);
This outputs:
2011-11-09 07:27:54.342 Craplet[45608:707] dateString; 2011-09-11 05:22:02 +0000
2011-11-09 07:27:54.343 Craplet[45608:707] dateString; (null)

Trouble printing NSDate returned from formatter

I want to see what is getting stored in an NSDate, so I am using NSLog, but it's showing (null), whereas if I print the string stf2, it's showing the proper value.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"YYYY-MM-dd"];
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMM dd, yyyy"];
NSString *stf2 = [[pact.date componentsSeparatedByString:#" "] objectAtIndex:0];
NSLog(#"date %#",stf2);
NSDate *date_ = [formatter dateFromString:stf2];
pact.date = [formatter1 stringFromDate:date_];
NSLog(#"date %#",[NSDate date_]);
There are two specific problems in the code you've presented in the question.
Format Reset
First you do,
[formatter setDateFormat:#"YYYY-MM-dd"];
and then you initialize the second formatter followed by resetting the first formatter's format,
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMM dd, yyyy"];
To emphasize
[formatter setDateFormat:#"MMM dd, yyyy"];
This should've been formatter1 but is formatter.
Date Format
If you look at the format you've use YYYY-MM-dd, it looks fine. But apparent YYYY have a different purpose and can be different from our usual calendar year. You should use the lowercase y instead.
[formatter setDateFormat:#"yyyy-MM-dd"];
And I don't think you meant this but
NSLog(#"date %#",[NSDate date_]);
should be
NSLog(#"date %#", date_);
you need to correct the dateformatter by setting proper date formatter. first do this
[formatter setDateFormat:#"yyyy-dd-MM"];
//it should be in the way as your string is. like if your string is 2011-Jun- 27 then fromatter should be
[formatter setDateFormat:#"yyyy-MM-dd"];
set the formatter as per your string's date format. then get the date back from this line
NSDate *date_ = [formatter dateFromString:stf2];
Assuming "stf2" is your string, then perhaps your object formatter is nil.
Below functions will be helpful to you.
"getDateTimeFromString" will take date and time as argument and it will return NSDate object.\
-(NSDate *)getDateTimeFromString :(NSString *)tempDate :(NSString *)tempTime{
NSString *dateValue = [tempDate stringByAppendingFormat:#" %#",tempTime];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateValue];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:date];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:date];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:date] autorelease];
return date;
}
"getDateStringFromDate" will take NSDate as argument and it will return NSString.
So, you can NSLog that value.
-(NSString *)getDateStringFromDate :(NSDate *)dateValue{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setTimeStyle:NSDateFormatterShortStyle];
[timeFormat setDateFormat:#"HH:mm a"];
NSString *theDate = [dateFormat stringFromDate:dateValue];
/*NSLog(#"\n"
"theDate: |%#| \n"
"theTime: |%#| \n"
, theDate, theTime);*/
return theDate;
}
Hope you will get the answer.

How to get day and date?

i want to get date in 2 labels like this
Label1:-> Tuesday
Label2:-> 2 June 2010
how can i get this value?
This code will get you the strings you require to set the labels:
NSDate *today = [NSDate date];
NSDateFormatter *weekdayFormatter = [[[NSDateFormatter alloc] init] autorelease];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat: #"d MMMM yyyy"];
[weekdayFormatter setDateFormat: #"EEEE"];
NSString *formattedDate = [formatter stringFromDate: today];
NSString *weekday = [weekdayFormatter stringFromDate: today];
The date formatters are controlled through this standard: Unicode Date Formats
[label1 setText:#"Tuesday"];
[label2 setText:#"2 June 2010"];
Or did you need something different?
Hello You can get Day and Date From this code
and here weekday give you the day Example: Monday,Tuesday for re
NSDate *today = [NSDate date];
NSDateFormatter *weekdayFormatter = [[[NSDateFormatter alloc] init] autorelease];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat: #"d MMMM yyyy"];
[weekdayFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[weekdayFormatter setDateFormat: #"EEEE"];
NSString *formattedDate = [formatter stringFromDate: today];
NSString *weekday = [weekdayFormatter stringFromDate: today];
In swift, you can do like that:
let today = Date()
let weekdayFormatter = DateFormatter()
let formatter = DateFormatter()
formatter.dateFormat = "d MMMM yyyy"
weekdayFormatter.dateFormat = "EEEE"
let formattedDate = formatter.string(from: today)
let weekday = weekdayFormatter.string(from: today)