NSString to NSDate conversion for a specific format - objective-c

I receive NSString(s) in the following format:
2013-05-18T17:37:06.9419518Z
How do I convert the date into a NSDate properly so I can compare it with another NSDate that contains the current date/time
This is what I have right now:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZ"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:extractString];
NSLog (#"dateFromString: %#", dateFromString);
[dateFormatter release];
[dateFromString release];
but I get a (null) output in the log out of it
thank you.

If the 'Z' is a constant at the end of your time, then you need to put it in single quotes in the date format.
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSSSSS'Z'"];

Related

Converting Date to proper format when the Value obtained from the Db is in format 2012-07-13 00:00:00.0 [duplicate]

This question already has answers here:
Formatting NSDate into particular styles for both year, month, day, and hour, minute, seconds
(8 answers)
Closed 9 years ago.
I know there are lot of questions on this topic . But I am not getting a proper solution to my
problem.I am retrieving Date from db , I am getting the Output as
as 2012-07-13 00:00:00.0 .
I am getting the output in the string format . I want the result of type string in the
format MM/dd/yyyy.I am doing the following
NSString *dateStr=#"2012-07-13 00:00:00.0";
NSDateFormatter *df=[NSDateFormatter new];
[df setDateFormat:#"yyyy-MM-dd"];
NSDate *res=[df dateFromString:dateStr];
But here I am getting res as nil .
You need to tell the formatter the whole format of the date, including hours, minutes...
This works:
NSString *dateStr=#"2012-07-13 00:00:00.0";
NSDateFormatter *df=[NSDateFormatter new];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss.S"];
NSDate *res=[df dateFromString:dateStr];
[df setDateFormat:#"MM/dd/yyyy"];
NSString *myString =[df stringFromDate:res];
Use this one
NSString *dateStr=#"2012-07-13 00:00:00.0";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss.S"];
NSDate *date = [df dateFromString: dateStr];
[df setDateFormat:#"MM/dd/yyyy"];
NSString *convertedString = [df stringFromDate:date];
NSLog(#"Your String : %#",convertedString);
Output:
Your String : 07/13/2012
try this:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.s"];
NSDate *aDate=[formatter dateFromString:[NSString stringWithFormat:#"%#", #"2012-07-13 00:00:00.0"]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
NSLog(#"%#", [dateFormatter stringFromDate:aDate]);
Try this
NSString *dateStr=#"2012-07-13 00:00:00.0";
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss.S"];
NSDate *dateObj=[dateFormatter dateFromString:dateStr];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSString *myString = [dateFormatter stringFromDate:dateObj];
NSLog(#"%#",myString);

Convert NSString in 12 hour time format

In my project I have an NSString that contains data like this
NSString *s=#"20";
I want to convert this as "08:00 ". How can I do this?
I tried this one:
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"hh:mm a"];
NSString* dateString = [dateFormatter stringFromDate:#"20"];
but I am getting this dateString as null value.
You input as NSString, need output as NSString.
So intermediate work of NSDate and NSDateFormatter are not required.
You can attain this by :
NSString *s=#"20";
NSString *time=[NSString stringWithFormat:#"%d:00",[s integerValue]%12];
Please try to use this one .I hope it will work fine...
NSString *s=#"20";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"HH"];
NSDate *date = [df dateFromString:s];
[df setDateFormat:#"hh:mm a"];
NSString *newTime = [df stringFromDate:date];
NSLog(#"Time : %#",newTime);

Time formats for NSDate

I have datetime values coming into my app from a web service. The format is as follows:
2013-03-24T09:45:00.000-04:00
I need only the time from this value. I am using NSDateFormatter and am setting the date format as follows:
[df setDateFormat:#"HH:mm:ss"];
and when I do this I get a rather cryptic runtime error. However, I have no errors when I use the following (though it doesn't satisfy my requirement):
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.sssz"];
Can someone help?
Try this
NSString *originalDateString = #"2013-03-24T09:45:00.000-04:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.sssz"];
NSDate *date = [dateFormatter dateFromString:originalDateString];
[dateFormatter setDateFormat:#"HH:mm:ss"];
NSString *newDateString = [dateFormatter stringFromDate:date];
Maybe if you use date formater to retrieve a NSDate instance and from there do you own formating.
NSSTring *dateString = "2013-03-24T09:45:00.000-04:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *dateFromWebApp = [dateFormatter dateFromString:dateString];
Haven't tested tho but it should work.
Cheers

Turning NSDate into NSString [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert NSDate to NSString
convert string to nsdate
Currently I have this code. It's for adding events to the calendar.
[...]
event.startDate = [[NSDate date] dateByAddingTimeInterval:86400];
event.endDate = [[NSDate date] dateByAddingTimeInterval:90000];
What I need is the code to add to a spesific start date and end date, and that's where NSString comes in handy. But I've had no luck converting it so far.
Refer this code :
NSString to NSDate
NSString *dateString = #"01-02-2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSDate convert to NSString:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"%#", strDate);
[dateFormatter release];
Hope it helps you
convert NSDate to NSString as
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSString *string = [dateFormatter stringFromDate:date];
[dateFormatter release];
That's an example if you need the format like “Nov 23, 1937”:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateStyle= NSDateFormatterMediumStyle;
NSString* string= [formatter stringFromDate: date];
Check out the reference for other styles. If you need another style that hasn't a constant, you can use the date format, in this case it's:
formatter.dateFormat= #"MMM dd, yyyy"; // same as medium style
But the preferred way is to use the style, use the format only if there isn't a propert style.

How to convert a DD-MM-YYYY date format string into a YYYY-MM-DD date format string or into a NSDate object in Objective-C?

I have read date from XML which give me back string where the date is like DD-MM-YYYY. But when I want to add it to my core data database, SQLite sorts me that wrong so I have to convert it to date format or to string like: YYYY-MM-DD. But SQLite does not support either Date() or To_Date() functions. Can you help me?
You can use NSDateFormatter to format a date in Objective C.
Here's a quick example which might not do exactly what you want, but should hopefully give some pointers:
// Convert my dd-mm-yyyyy string into a date object
NSString *stringDate = #"01-02-2011";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [dateFormatter dateFromString:stringDate];
// Convert my date object into yyyy-mm-dd string object
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString *formattedStringDate = [dateFormatter stringFromDate:date];
[dateFormatter release];
Hope this helps!
Nick.
Using Nick's tutorial and answer the solution is :
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd.MM.yyyy"];
NSDate *date = [dateFormatter dateFromString:#"01.02.1989"];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
// Convert my date object into yyyy-mm-dd string object
[dateFormatter1 setDateFormat:#"yyyy-MM-dd"];
NSString *formattedStringDate = [dateFormatter1 stringFromDate:date];
[dateFormatter release];
[dateFormatter1 release];
NSLog(formattedStringDate);
objektObrat.dateOfObrat = [[NSString alloc] initWithString:formattedStringDate];