how can get date from string ? my code is enclosed here by - ios

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
NSDate *date1 =[[NSDate alloc] init];
date1 = [dateFormat dateFromString:#"11-nov-2013"];
NSLog(#"date is %#",date1); // it returns null
self.datePicker.date = date1;
[dateFormat release];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MMM-yyyy"];
NSDate *myDate = [formatter dateFromString:#"11-nov-2013"];
NSLog(#"date is %#",myDate);

Set date format to
[dateFormat setDateFormat:#"dd-MM-yyyy"];

You need to set the date format as well :
[dateFormat setDateFormat:#"dd-MM-yyyy"];

Related

objective-c dateFromString returns nil on specific date

The date is always nil, but if I change the dateString to any other date like 03/01/1975 it works normally. Any idea?
NSString *dateString = #"05/01/1975";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"us"];
[dateFormatter setLocale:gbLocale];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSDate *date = [dateFormatter dateFromString:dateString];
Try this
NSString *dateString = #"05/01/1975";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[formatter setDateFormat:#"MM.dd.yyyy"];
NSDate *anotherDate = [formatter dateFromString:dateString];
NSLog(#"%#",anotherDate);
It will give output like this
1975-05-01 00:00:00 +0000
try this code
- (NSDate *)getPickerDateForString:(NSString *)dateString
{
NSDate * dt;
if (!dateFormatter)
{
dateFormatter = [[NSDateFormatter alloc] init];
}
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
dt = [dateFormatter dateFromString:dateString];
return dt;
}

While converting NSString to NSDate?

I try to convert NSString to NSDate with this code:
NSString *dateString=[[NSString alloc] init];
dateString = [[messageArray objectAtIndex:i] tim_dte];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm,yyyy/MM/dd"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
msg.date = dateFromString;
But msg.Date is nil even if dateString shows the correct date string. What am I missing?
change this format
[dateFormatter setDateFormat:#"HH:mm,yyyy/MM/dd"];
into and try
[dateFormatter setDateFormat:#"yyyy/MM/dd HH:mm"];
Update
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy/MM/dd HH:mm"];
// use simple
msg.date = [dateFormatter dateFromString:#"2016/05/06 12:36"];
NSLog(#"final date == %#",dateFromString);
//NSDate *dateFromString = [dateFormatter dateFromString:#"2016/05/06 12:36"];
// NSLog(#"final date == %#",dateFromString);
Try this code to convert NSString to NSDate
NSString *dateString = #"2016-05-06";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [dateFormatter dateFromString:#"2013-02-01T06:25:47Z"];
NSTimeZone *pdt = [NSTimeZone timeZoneWithAbbreviation:#"PDT"];
[dateFormatter setTimeZone:pdt];
[dateFormatter setDateFormat:#"HH:mm:ss zzz"];
[dateFormatter setDateFormat:#"K:mm a, z"];
NSString * updated String = [dateFormatter stringFromDate:date];
Use this Code,
NSString *dateString=[[NSString alloc] init];
dateString =[[messageArray objectAtIndex:i] tim_dte];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy/MM/dd HH:mm"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
msg.date=dateFromString;
hope its helpful

NSDateFormatter dateFromString with format 'hh:mm a' return nil

i came across a problem in my iPhone application.
in my app i converte time string to date like that
NSString *time = #"01:30 PM";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"hh:mm a"];
NSDate *date = [dateFormat dateFromString:time];
and for some reason always date is nil after [dateFormat dateFromString:time].
can anyone tell me what i'm doing wrong?
thanks
I'm assuming your device language is other then English. In that case you need to set the date formatter Locale. Try this code.
NSString *time = #"01:30 PM";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"hh:mm a"];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
try this...
NSString *time = #"01:30 pm";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeStyle:NSDateFormatterShortStyle];
[dateFormat setDateFormat:#"hh:mm a"];
dateFormat.timeZone = [NSTimeZone timeZoneWithName:#"GMT"];
NSDate *date = [dateFormat dateFromString:time];
NSLog(#"date %#",[dateFormat stringFromDate:date]);
NSString *dats1 = #"01:30:00 PM";
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init] ;
[dateFormatter1 setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter1 setDateFormat:#"HH:mm:ss"];
NSDate *date = [dateFormatter1 dateFromString:dats1];
NSLog(#"date : %#", date);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"hh:mm a"];
NSLog(#"Current Date: %#", [formatter stringFromDate:date]);

NSDateFormatter date format type

I am trying to set the date format for something like "2013-12-02T12:15:43.182Z. I used that code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ssS''Z'"];
How can i set this date format?
Ues this:
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *dateConverted = [dateFormatter dateFromString:#"2013-12-02T12:15:43.182Z"];
NSLog(#"%#",dateConverted);
This might work for you..
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
This may help you,
NSDate *myDate = [NSDate date];
NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSLog(#"Required date format: %#",[df stringFromDate:myDate]);

convert the time to a specific format

I have the current time format returned from the web service:
2012-06-25T23:45:52.664Z
I want to get something like:
2012 june 25 23:45
So without seconds and the Z format indicator.
My relevant code is:
myConvertedTime = [[myActualTime dateFromString] stringFromDate];
try these
NSDate *dateTemp = [[NSDate alloc] init];
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:#"yyyy MMM dd hh:mm"];
dateTemp = [dateFormat1 dateFromString:newInvoice.date];
You should use an NSDateFormatter object. And rather than specify a format yourself, it's best to let the framework pick the one that suits based on the user's internationalization settings. This is done as follows:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
myConvertedTime = [formatter stringForObjectValue:[myActualTime dateFromString]];
[formatter release];
try this:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy MMMM dd HH:mm"];
NSString *myConvertedTime = [formatter stringFromDate:now];
Try the following code :
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss"];
NSString *dateString = #"2012-06-25T23:45:52.664Z";
dateString = [dateString substringToIndex:[dateString length]-5];
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"yyyy MMMM dd HH:mm"];
NSString *dateText = [dateFormatter2 stringFromDate:date];
[dateFormatter2 release];
NSLog(#"the date is:%#",dateText);

Resources