I have created a below method in which i can send dateString, InputFormat & OutputFormat aswell as below.
- (NSString *)InstanceAwesomeFormatterunformattedDate:(NSString*)unformattedDate inputFormat:(NSString*)inputFormat outPutFormat:(NSString*)outPutFormat{
//unformattedDate is 13 july 1989
//inputFormat is #"dd MMMM yyyy"
//outPutFormat is #"dd"
NSDateFormatter *myDateFormattr = [[NSDateFormatter alloc] init];
[myDateFormattr setDateFormat:inputFormat];
NSDate *date = [myDateFormattr dateFromString:unformattedDate];
//Why date is 1990-07-12 18:30:00 +0000
[myDateFormattr setDateFormat:outPutFormat];
NSString *FinalExpiryDateForUserProfile = [myDateFormattr stringFromDate:date];
// FinalExpiryDateForUserProfile is 13 which is correct
return FinalExpiryDateForUserProfile;
}
I am trying to understand why date prints 12 instead of 13.
Any ideas
You need to use locale
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd"];
NSLocale *enLocale = [NSLocale localeWithLocaleIdentifier:#"en_US"];
[dateFormatter setLocale:enLocale];
Problem is with time zone and you can set timezone like this:
NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
[myDateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
You can add any timezone instead of GMT as per your requirement. Setting NSLocale is not really mandatary if you set proper timezone as per your settings.
Related
I have stacked with dateFormat of 2017-12-13T10:30:00.000-06:00 for NSDateFormatter. I'd tried different formats but still getting nil from [formatter dateFromString:exampleDate]. The most possible format I had picked up on nsdateformatter was #"yyyy-MM-dd'T'HH:mm:ss.SSSZ" but TimeZone not applying correctly and I got nil from NSDateFormatter.
I also try to use:
NSISO8601DateFormatter *dateFormatter = [[NSISO8601DateFormatter alloc] init];
[dateFormatter dateFromString:#"2017-12-13T10:30:00.000-06:00"];
and also got nil.
You can use NSISO8601DateFormatter with options .withFractionalSeconds (iOS 11.0+) and .withInternetDateTime
NSISO8601DateFormatter *dateFormatter = [[NSISO8601DateFormatter alloc] init];
dateFormatter.formatOptions = NSISO8601DateFormatWithFractionalSeconds | NSISO8601DateFormatWithInternetDateTime;
NSDate *date = [dateFormatter dateFromString:#"2017-12-13T10:30:00.000-06:00"];
NSLog(#"%#", date);
For a standard NSDateFormatter the date format is correct
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSZ";
NSDate *date = [dateFormatter dateFromString:#"2017-12-13T10:30:00.000-06:00"];
NSLog(#"%#", date);
I'm trying to format a date in objective-c for an ios app in this format:
2017-02-15T16:32:59.9725843+11:00
with no luck. I've tried:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone localTimeZone]];
formatter.dateFormat = #"yyyy-MM-dd'T'hh:mm:ss.SSSSSSSZZZZZ";
and
formatter.dateFormat = #"yyyy-MM-dd'T'hh:mm:ss.fffffffZZZZZ";
As well as a bunch of other formats for the milliseconds and timezone (I've varied the number of Ss and Zs with no luck. All I get is a nil NSString.
Any help would be appreciated.
Your your String Time format is 24 hour, so use HH:mm:ss. in this place hh:mm:ss.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone localTimeZone]];
formatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZZZZZ";
NSDate *getDate = [formatter dateFromString:#"2017-02-15T16:32:59.9725843+11:00"];
NSLog(#"getDate == %#",getDate);
output:
It's .SSS, for millisecond check also this Unicode Locale Data Markup Language spec
Try this code for milliseconds :
"yyyy-MM-dd'T'HH:mm:ss.SSS"
You can also check nsdateformatter and DataFormatting
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setTimeZone:[NSTimeZone localTimeZone]];
dateformatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZZZZZ";
NSString *currentDate = [dateformatter stringFromDate:[NSDate date]];
NSLog(#"current Date :%# ",currentDate);
Output:
current Date :2017-02-15T11:35:11.7790000+05:30
This is really odd, the code below takes self.danceTimeIn (its text state) and converts it to an actual time. The problem is that its coming up 1 hour LESS than what's entered. Meaning that if I enter 14:03 I'll get 13:03 in the database! The same thing is happening with the date version of this code
++++++++++++++++++++++
TIME
NSString *danceTimeIn = self.danceTimeIn.text;
NSDateFormatter *timeFormatIn = [[NSDateFormatter alloc] init];
[timeFormatIn setDateFormat:#"HH:mm"];
NSDate *timeIn = [timeFormatIn dateFromString: danceTimeIn];
DATE
NSString *danceDateValue = self.danceDate.text;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yy"];
NSDate *date = [dateFormat dateFromString: danceDateValue];
++++++++++++++++++++++
Anyone ???
Try using this. NSDateFormater change date according to locale of your device settings, if you set locale properlyl, you will get proper date. Try if this works :)
NSString *danceTimeIn = self.danceTimeIn.text;
NSDateFormatter *timeFormatIn = [[NSDateFormatter alloc] init];
[timeFormatIn setDateFormat:#"HH:mm"];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[timeFormatIn setLocale:usLocale];
NSDate *timeIn = [timeFormatIn dateFromString: danceTimeIn];
Time zone may be causing this problem.Try
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
May be this will help.
It depends on the timezone:-
first Check your local time zone
NSTimeZone *tz=[NSTimeZone timeZoneWithName:#"GMT"];
[dateformat setTimeZone:tz];
and then set your date accordingly.
dateFromString returns me null when I am trying to convert NSString to NSDate
here is my code:
NSString *dateString = #"Wed 16 Oct 2013";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale systemLocale]];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:#"EEE dd MMM yyyy"];
[dateFormatter setFormatterBehavior:NSDateFormatterBehaviorDefault];
NSDate *myDate = [dateFormatter dateFromString: dateString];
Please, give me any suggestions about this.
It's a locale issue and it's sort of the same of NSDateFormatter Strings in iOS 7, just the other way around.
Probably in your system locale Wed or/and Oct are not valid valid literals for a weekday or/and a month.
Try setting the locale to en_US or to currentLocale
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
or
NSLocale *locale = [NSLocale currentLocale];
For more formatting style http://cybersam.com/ios-dev/quick-guide-to-ios-dateformatting
NSString *dateString = #"Wed 16 Oct 2013";
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:#"EEE dd MMM yyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSLog(#"%#", dateFromString);
I have time in NSString like "15:15"
I want to covert this NSString time into 12 hr NSString(i.e "3:15 PM").
Use NSDateFormatter to turn the string in a NSDate. Then use the dateformatter again to change the NSDate to a string.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"HH:mm";
NSDate *date = [dateFormatter dateFromString:#"15:15"];
dateFormatter.dateFormat = #"hh:mm a";
NSString *pmamDateString = [dateFormatter stringFromDate:date];
Code might contain some errors, written without compiling or testing.
#python
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:locale];
[dateFormatter setDateFormat:#"hh:mm"];
Hope this helps!
Check out this SO question: NSDateFormatter dateFromString and iPhone in 24 Hour Format Confusion
You should be able to use to use NSDateFormatter to format the style of time you want, then read in the string to the formatter and pull it back out in your desired format.