Convert NSString to NSDate in objective c [duplicate] - objective-c

This question already has answers here:
Converting NSString to NSDate (and back again)
(17 answers)
Closed 5 years ago.
I am getting a NSString as #"27-Jan-2018 00:00".i just want to convert this string to NSDate class.Kindly suggest some methods to achieve this.Thanks in advance!

Try this...
NSString *dateString = #"27-Jan-2018 00:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
[dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm"];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
NSLog(#"%#",dateFromString);

Please, check it below code. Maybe its work for you and your needs.
-(void)DateChange
{
NSString *Input_Date =#"31-Jan-18 04:25 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MMM-yy hh:mm a"];
NSDate * Format_date = [dateFormatter dateFromString:Input_Date];
[dateFormatter setDateFormat:#"dd-MMM-yy hh:mm a"];
NSString *Change_date = [dateFormatter stringFromDate:Format_date];
NSLog(#"Final Change Date :- %#",Change_date);
}
My Output is :-
Final Change Date :- 31-Jan-18 04:25 AM

Related

NSDate returning nil for the NSDateFormatter set correctly

I am getting the date in NSString format as mentioned below.
Input:datestring = 30/04/2013 04:49 PM
I am applying the format for NSDateformatter as mentioned below to convert this NSString to NSDate .
Format:dateFormat= dd/MM/yyyy hh:mm a
I still get the date conversion to Nil. Please help as I am not understanding this issue.Here is my code snippet to explain what i am doing in the utility function.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:dateFormat];
NSDate *date = [dateFormatter dateFromString:dateString];
return date;
Try This....
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy hh:mm a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDate *date = [dateFormatter dateFromString:#"30/04/2013 04:49 PM"];
NSLog(#"%#",date);

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.