NSDateFormatter returning Null - ios

I have this method on an object:
- (NSString *)formattedScheduledOn
{
NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init];
[dateFormater setDateFormat:#"MM/dd/yyyy hh:mm"];
return [dateFormater stringFromDate:self.scheduledOn];
}
scheduledOn is an NSDate object. Showing it as a raw date yields something like: 2013-03-22T09:44:28-07:00
But I always get null from that method. Any ideas? Thanks!

Make sure your date is not nil.
Try the following code and see what you get in return
NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[dateFormater setDateFormat:#"MM/dd/yyyy hh:mm"];
return [dateFormater stringFromDate:[NSDate date]];
If change you passing the date as string then you should set the format according to the date string passed.
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssz"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];

Why not do something like this?
Set your datePicker to pick or whatever you want.
[pick addTarget:self action:#selector(updateDateLabel:) forControlEvents:UIControlEventValueChanged];
-(IBAction)updateDateLabel:(id)sender {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
[formatter setTimeStyle:NSDateFormatterMediumStyle];
dateLabel.text = [formatter stringFromDate:pick.date];
}

Related

How to convert NSString to a date formattted string

I have string with value 01012016 which i want to show to user like 01-01-2016. For this, i am using the below code
-(NSString *)dateToFormatedDate:(NSString *)dateStr {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
[dateFormatter setDateFormat:#"MM-dd-yyyy"];
NSDate *date = [dateFormatter dateFromString:dateStr];
NSLog(#"date is %#", [dateFormatter stringFromDate:date]);
return [dateFormatter stringFromDate:date];
}
But this method is always returning nil. I have tried on simulator and device both.
I am not able to figure it out what i did wrong here
The format of your NSDateFormatter does not match the string you're trying to pass it. Take a look at this related question...
Converting a string to an NSDate
What you want to do is set the NSDateFormatter to match the string, then once you've created an NSDate object from the string, change the NSDateFormatter to your desired output format. Something like this...
-(NSString *)dateToFormatedDate:(NSString *)dateStr {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
[dateFormatter setDateFormat:#"MMddyyyy"];
NSDate *date = [dateFormatter dateFromString:dateStr];
[dateFormatter setDateFormat:#"MM-dd-yyyy"];
NSLog(#"date is %#", [dateFormatter stringFromDate:date]);
return [dateFormatter stringFromDate:date];
}

How to reuse a NSString method?

I am writing a method that takes string and returns string. I am trying to use it in another method.
- (NSString*) formatDate:(NSString*) showStartDateTime
{
NSTimeZone *tz = [NSTimeZone timeZoneWithName:#"UTC"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:tz];
[dateFormatter setDateFormat:#"EEE MM/dd/yyyy"];
NSDate*dattt=[dateFormatter dateFromString:showStartDateTime ];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString*tempo2= [dateFormatter stringFromDate:dattt];
return tempo2;
}
This is how I am using it. [self formatDate:#"Fri 08/08/2014"] is it a correct way to use it?
NSString *hellostring=[self formatDate:#"Fri 08/08/2014"];
NSLog(#"%#",hellostring);
I know it does not work. Any suggestion is appreciated.
I wrote a command line app and wasnt able to get it running, as already dateFromString: resulted in nil, until I added dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
- (NSString*) formatDate:(NSString*) showStartDateTime
{
NSTimeZone *tz = [NSTimeZone timeZoneWithName:#"UTC"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:tz];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setDateFormat:#"EEE MM/dd/yyyy"];
NSDate*dattt=[dateFormatter dateFromString:showStartDateTime ];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString*tempo2= [dateFormatter stringFromDate:dattt];
return tempo2;
}
Now it returns 2014-08-08
see Technical Q&A QA1480: NSDateFormatter and Internet Dates

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

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"];

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