NSString *lower = [NSString stringWithFormat:#"%#",[newDates objectAtIndex:0]];
NSString *higher = [NSString stringWithFormat:#"%#",[newDates objectAtIndex:[newDates count]-1]];
NSLog(#"%#",lower);
NSLog(#"%#",higher);
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd"];
NSDate *dtLower = [df dateFromString:lower];
NSDate *dtHigher = [df dateFromString:higher];
[df release];
NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
[df1 setDateFormat:#"yyyy-MM-dd"];
NSString * lowerDate = [df1 stringFromDate:dtLower];
NSString * higherDate = [df1 stringFromDate:dtHigher];
UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 31, self.view.frame.size.width, 30)];
[dateLabel setTextColor:[UIColor grayColor]];
dateLabel.text = [NSString stringWithFormat:#"%# - %#",lowerDate,higherDate];
[self.view addSubview:dateLabel];
Output of lower and higher in Console is:
2011-05-23 14:55:52.767 Vocab[4225:207] 2011-05-23 03:58:22
2011-05-23 14:55:53.781 Vocab[4225:207] 2011-05-23 07:14:56
Here the above code does not work for me. The value for higher and lower are not null but when I try converting it into NSDate using NSDateFormatter then it returns nil.
So dtLower and dtHigher return nil
What could be wrong?
Try setting [df setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
Your first date format is wrong.
If you're working with user-visible dates, you should avoid setting a date format string as it's very hard to predict how your format string will be expressed in all possible user configurations. Rather, you should try and limit yourself to setting date and time styles (via -[NSDateFormatter setDateStyle:] and -[NSDateFormatter setTimeStyle:]).
If you're working with fixed-format dates, you should first set the locale of the date formatter to something appropriate for your fixed format. In most cases the best locale to choose is en_US_POSIX, a locale that's specifically designed to yield US English results regardless of both user and system preferences. en_US_POSIX is also invariant in time and between machines (en_US_POSIX works the same on iOS as it does on OSX, and as it it does on other platforms).
Once you've set en_US_POSIX as the locale of the date formatter, you can then set the date format string and the date formatter will behave consistently for all users.
The above info and more can be found in Apple's Technical Q&A QA1480
Here's a snippet of code which implements the recommendation from the above Technical Note:
static NSDateFormatter* dateFormatter = nil;
if (!dateFormatter)
{
dateFormatter = [NSDateFormatter new];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc]
initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
dateFormatter.dateFormat = #"EEE, dd MMM yyyy HH:mm:ss +0000";
}
Related
I have a UIDataPicker in my viewController with default location, when my user finishes selecting the date I run this code:
NSString *dateString = [NSDateFormatter localizedStringFromDate:[self.dataPicker date]
dateStyle:NSDateFormatterLongStyle
timeStyle:NSDateFormatterNoStyle];
With that code I can storage the date in the following format:
May 31, 2016
Later in my code I need to convert this string into a real date format, for this I use the code below:
-(NSDate*)convertStringToDate:(NSString*)date{
NSString *dateString = date;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
return [dateFormatter dateFromString:dateString];
}
But this code return a null value. As the datepicker is set by default, my system can receive any date format, but in the end I want it to be converted to the format en_us.
How I can solve this problem?
Don't store the date as a string; store it as an offset, in seconds, from some reference date.
i.e:
uint64_t offset = (uint64_t)[[self.dataPicker date] timeIntervalSinceReferenceDate];
// store this 64-bit unsigned integer.
This takes less space and is quicker to convert to/from an NSDate object.
You can leave the offset as an NSTimeInterval (64-bit floating point double) if you prefer, but as you aren't storing date & time, uint64_t should do...
Use this code,
-(NSDate*)convertStringToDate:(NSString*)date{
NSString *dateString = date;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setDateFormat:#"MMM d, yyyy"];
return [dateFormatter dateFromString:dateString];
}
hope its helpful
The formatting string depends on the locale you are using. From the localizedStringFromDate documentation:
Returns string representation of a given date formatted for the
current locale using the specified date and time styles.
This method uses a date formatter configured with the current default
settings. The returned string is the same as if you configured and
used a date formatter as shown in the following example:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.formatterBehavior = NSDateFormatterBehavior10_4;
formatter.dateStyle = dateStyle; formatter.timeStyle = timeStyle;
NSString *result = [formatter stringForObjectValue:date];
Means, you should do the next:
-(NSDate*)convertStringToDate:(NSString*)dateString {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.formatterBehavior = NSDateFormatterBehavior10_4;
dateFormatter.dateStyle = NSDateFormatterLongStyle;
dateFormatter.timeStyle = NSDateFormatterNoStyle;
return [dateFormatter dateFromString:dateString];
}
How to convert a date of any language(Arabic) to a specific language(English).
While changing the region format to arabic, the dates are getting changed. When I am picking the date value from some controls(UIButtons or UILabels) for saving in database, its picking the date in arabic language and saving in the same format. Here I want to convert the date from arabic to english before saving it in database.
I tried this but its returning nil
NSDate *currentDate = //My date coming like “٢٠١٤-٠٥-١٥ ١٢:١٠:٢٣ ”
NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[formatter setLocale: usLocale];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:s a"];
NSString *formattedDate = [formatter stringFromDate: currentDate];
Can anyone please help me ?
I solved in this way
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:s"];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setLocale:usLocale];
NSString *dateString = [dateFormatter stringFromDate:myDate];
You shouldn't store dates in any language. You should store NSDate, which has no language or locale or time zone, and display it as appropriate. If your database cannot store NSDate, you can store [myDate timeIntervalSince1970] and convert it to an NSDate using the class method dateWithTimeIntervalSince1970:
You should use UIDatePicker as the input instead of UIButtons or UILabels. Save the timeIntervalSince1970 into your database, so that it can be presented by any language.
NSDate *currentDate = //My date coming like “٢٠١٤-٠٥-١٥ ١٢:١٠:٢٣ ”
NSTimeInterval timeToBeSavedInDatabase = [currentDate timeIntervalSince1970];
//in the future, you can show this date in any language
NSString *dateString = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970]];
You may use this to get the date with preferred local
NSDate * dateNow = [NSDate new];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormat setLocale: usLocale];
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
NSString * dateString = [dateFormat stringFromDate:dateNow];
The following code will set date to nil.
NSString *dateString = #"2014-04-27T04:20:07.000-04:00";
NSString *UTC_FORMAT = #"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:UTC_FORMAT];
NSDate *date = [formatter dateFromString:dateString];
What am I doing wrong?
I've tried many other different variations for UTC_FORMAT, but counldn't seem to get it. I'm also a little bit confused as to when and where the single quotes go. After playing with this for a while, I'm assuming it can goes around characters that shouldn't be interpreted by the formatter, but that's a separate thing.
Related Links That Couldn't Help Me:
Apple Docs: Data Formatting Guid
SO: Why is NSDateFormatter returning nil?
Formats That I've Tried:
NSString *UTC_FORMAT = #"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'";
NSString *UTC_FORMAT = #"yyyy-MM-dd'T'HH:mm:ss-Z";
Your date looks like a quite standard JSON date format in RFC3339 format. However, there are several possibilities how these dates can be formatted. In this case, your date string contains milliseconds. Your date format doesn't, so this cannot work. The following code will check for dates without fractional seconds first, then for dates with fractional seconds. Furthermore, you are looking for a literal character Z instead of a timezone.
The "X5" is documented at
http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns
and converts time zones in quite a flexible way, including the colon in the middle. .SSSSSS will convert fractional parts of seconds up to microseconds. Should you be given nanoseconds change it to nine S characters.
And I forgot the locale information...
NSString *UTC_FORMAT = #"yyyy'-'MM'-'dd'T'HH':'mm':'ssX5";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:UTC_FORMAT];
enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
gmtTimeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
[formatter setLocale:enUSPOSIXLocale];
[formatter setTimeZone:gmtTimeZone];
NSDate *date = [formatter dateFromString:dateString];
if (date == nil)
{
NSString *UTC_FORMAT2 = #"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSSSSSX5";
NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat:UTC_FORMAT2];
[formatter2 setLocale:enUSPOSIXLocale];
[formatter2 setTimeZone:gmtTimeZone];
date = [formatter2 dateFromString:dateString];
}
To avoid dependencies on the current locale, add :
NSString *dateString = #"2014-04-27T04:20:07.000-04:00";
NSString *UTC_FORMAT = #"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:UTC_FORMAT];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
NSDate *date = [formatter dateFromString:dateString];
I am using below code.
NSString * weekdayString = [[self date] descriptionWithLocale:#"%A"];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:usLocale];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
dateString = [NSString stringWithFormat:#"%#,%#",weekdayString,[dateFormatter stringFromDate:[self date]]];
NSArray * components = [dateString componentsSeparatedByString:#","];
dateString = [NSString stringWithFormat:#"%#,%#",[components objectAtIndex:0],[components objectAtIndex:1]];
it's working fine in simulator and ios 6.0 but it's not showing as expected in ios 5.1 in device(iPad).
NSDate -descriptionWithLocale is documented to take an NSLocale; you're passing an NSString and therefore relying on undocumented behaviour.
It's also unclear what you're trying to achieve with the splitting into components — presumably you've assumed that the NSDateFormatterShortStyle will always have a comma in it? That also isn't guaranteed by the documentation.
What you probably want to do is just set a custom date format string. From the title of your post it looks like you probably want EEEE, MMMM dd. So e.g.
// see QA1480 re: en_US_POSIX
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:usLocale];
[dateFormatter setDateFormat:#"EEEE, MMMM dd"];
dateString = [dateFormatter stringFromDate:[self date]];
QA1480 addresses an issue that can cause NSDateFormatter silently to adapt your date formatter internally.
I am getting start and end dates for my calendar event(while parsing a .ics file) in "20110912T220000" format. How can I convert this to a NSDate to add to add as event(EKEvent)'s startDate property.
If anyone knows please help me soon.
You should use NSDateFormatter for this.
See Data Formatting Guide (the Date & Time Programming Guide may also be interesting)
This is also detailed in this Technical Note in Apple's Q&As. Note that for such situations, you should use the special "en_US_POSIX" locale as explained in this technical note.
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease]];
[df setDateFormat:#"yyyyMMdd'T'HHmmss"];
NSDate* parsedDate = [df dateFromString:...];
NSString *dateString = #"20110912T220000";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease];
formatter.locale = locale;
formatter.dateFormat = #"yyyyMMdd'T'HHmmss";
NSDate *date = [formatter dateFromString:dateString];
NSLog(#"date: %#", date);
NSLog() output: date: 2011-09-13 02:00:00 +0000
Note, NSLog outputs the data in you local timezone.
Note the single quotes around the 'T' in the date format.
Here is a link to UTS: date/time format characters