Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am adding the date and time to my iOS app. I have successfully added the date but want to show the time on a different label.
My code looks like this but I'm getting an error 'Redefinition of dateFormatter'. What can I do to call the time on a separate label?
NSLocale *en_US_POSIX = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
assert(en_US_POSIX != nil);
// The Date
//set the date label correctly
//format the date to July 9 2013 - MMMM d yyyy
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:en_US_POSIX];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// dateFromString = [dateFormatter dateFromString:curDate];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMMM d yyyy"];
NSString *theDate = [dateFormat stringFromDate:dateFromString];
labelDate.text = theDate;
// The Time
NSDate *currentTime = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh-mm"];
NSString *resultString = [dateFormatter stringFromDate: currentTime];
labelTime.text = resultString;
Your just need to delete the line where you are reinitializing the NSDateFormatter.
Correct code is :
NSLocale *en_US_POSIX = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
assert(en_US_POSIX != nil);
// The Date
//set the date label correctly
//format the date to July 9 2013 - MMMM d yyyy
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:en_US_POSIX];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// dateFromString = [dateFormatter dateFromString:curDate];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMMM d yyyy"];
NSString *theDate = [dateFormat stringFromDate:dateFromString];
labelDate.text = theDate;
// The Time
NSDate *currentTime = [NSDate date];
[dateFormatter setDateFormat:#"hh-mm"];
NSString *resultString = [dateFormatter stringFromDate: currentTime];
labelTime.text = resultString;
dude just change the NSDateFormatter name :) that's it, your problem solved :)
Try this
NSLocale *en_US_POSIX = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
assert(en_US_POSIX != nil);
//format the date to July 9 2013 - MMMM d yyyy
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:en_US_POSIX];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// dateFromString = [dateFormatter dateFromString:curDate];
[dateFormat setDateFormat:#"MMMM d yyyy"];
NSString *theDate = [dateFormat stringFromDate:dateFromString];
labelDate.text = theDate;
// The Time
NSDate *currentTime = [NSDate date];
[dateFormatter setDateFormat:#"hh-mm"];
NSString *resultString = [dateFormatter stringFromDate: currentTime];
labelTime.text = resultString;
Related
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;
}
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
This question already has answers here:
NSDateFormatter dateFromString Always Returns nil
(7 answers)
Closed 6 years ago.
I have the date in the following format:
NSString *dateString = #"1996-08-09T07:00:00Z";
I need to extract only the year from the date above:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setLocale:[NSLocale currentLocale]];
[dateFormat setDateFormat:#"yyyy"];
[dateFormat setFormatterBehavior:NSDateFormatterBehaviorDefault];
NSDate *date = [dateFormat dateFromString:dateString];
NSLog(#"date : %#", date);
Log:
2016-02-15 06:36:47.496 App[46714:5560483] date : (null)
I even tried:
[dateFormat setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]
but no avail.
set date format as
1996-08-09T07:00:00Z
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
update
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setLocale:[NSLocale currentLocale]];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
[dateFormat setFormatterBehavior:NSDateFormatterBehaviorDefault];
NSDate *date = [dateFormat dateFromString:#"1996-08-09T07:00:00Z"];
NSLog(#"date : %#", date);
[dateFormat setDateFormat:#"yyyy"];
NSString *finalStr = [dateFormat stringFromDate:date];
NSLog(#"finalStr : %#", finalStr);
output
Try this function:
+(NSDate *)convertUTCDateLocal:(NSString*)inputString
{
NSString *dateFormat = #"yyyy-MM-dd'T'HH:mm:sszz";
//for input
NSTimeZone *inputTimeZone =[NSTimeZone timeZoneWithName:#"UTC"];
NSDateFormatter *inputDateFormatter = [[NSDateFormatter alloc] init];
[inputDateFormatter setTimeZone:inputTimeZone];
//NSString *inputString = #"2015-07-31T14:00:00+0000";
[inputDateFormatter setDateFormat:dateFormat];
NSDate *date = [inputDateFormatter dateFromString:inputString];
//for output
NSDateFormatter *outputDateFormatter = [[NSDateFormatter alloc] init];
[outputDateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[outputDateFormatter setLocale:[NSLocale currentLocale]];
[outputDateFormatter setDateFormat: #"yyyy"];
NSString *outputString = [outputDateFormatter stringFromDate:date];
NSDate *outputDate = [outputDateFormatter dateFromString:outputString];
return outputDate;
}
The format:
yyyy
does not match the date string:
1996-08-09T07:00:00Z
hence [NSDateFormatter dateFromString:] returned nil.
You want this format:
yyyy-MM-dd'T'HH:mm:ssZ
(Note the X specifier is documented as being the correct specifier for Z (Zulu Time) however during a discussion with #rmaddy, he has convinced me that Z is the correct one to use.)
and then to extract the year using NSDateComponents.
I am trying to get date in the format Monday, March 09, 2015. But following code is not returning required date format. I think I am using wrong Formatter. Here is the code:
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMMM yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
try this...
//Getting date from string
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
// converting into our required date format
[dateFormatter setDateFormat:#"EEEE, MMMM dd, yyyy"];
NSString *reqDateString = [dateFormatter stringFromDate:date];
NSLog(#"date is %#", reqDateString);
LOG:2015-03-09 12:40:33.456 TestCode [1377:38775] date is Monday, March 09, 2015
Firstly you must convert your dateString to NSDatevalue
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
after that you could use this format: #"EEEE,MMMM dd, yyyy" to convert that dateValue to dateString like Monday, March 09, 2015
Hope this help
Helpful link for you
Try to set dateFormat property to #"dd'-'MM'-'yyyy" . Always check the unicode the date symbol table. "MMMM" means that the date month should be a full month name.
Try this one..
NSString * yourJSONString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];;
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSDate *dateFromString = [dateFormatter dateFromString:yourJSONString];
[dateFormatter setDateFormat:#"EEEE,LLLL dd, yyyy"];
NSString *output = [dateFormatter stringFromDate:dateFromString];
NSLog(#"%#", output);
Try this:-
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSString * format = [NSDateFormatter dateFormatFromTemplate:#"EEEEMMMMdyyyy" options:0 locale:[NSLocale currentLocale]];
[formatter setDateFormat:format];
NSString *dateFormatted = [fullDateFormatterTime stringFromDate: date];
NSLog(#"Formatted date: %#", dateFormatted);
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);