Future Relative dates human readable form - ios

How can I output human readable dates like turn NSDATE to human readable form like Tomorrow, one week from now, 1 month from now , etc?
For example if the current time is January 19, 2012 7 am
and the input date is January 20, 2012 9am
then the function should out Tomorrow at 9am.
I found how to do this with past dates online but not with future dates.
Also the functions I found online were not specific. Any help would be appreciated! thanks

You need to use an NSDateFormatter. From the docs:
setDoesRelativeDateFormatting: Specifies whether the receiver uses
phrases such as “today” and “tomorrow” for the date component.
- (void)setDoesRelativeDateFormatting:(BOOL)b
Parameters b YES to specify that the receiver should use relative date
formatting, otherwise NO. Discussion If a date formatter uses relative
date formatting, where possible it replaces the date component of its
output with a phrase—such as “today” or “tomorrow”—that indicates a
relative date. The available phrases depend on the locale for the date
formatter; whereas, for dates in the future, English may only allow
“tomorrow,” French may allow “the day after the day after tomorrow,”
as illustrated in the following example.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSLocale *frLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"fr_FR"];
[dateFormatter setLocale:frLocale];
[dateFormatter setDoesRelativeDateFormatting:YES];
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:60*60*24*3];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"dateString: %#", dateString);
// Output
// dateString: après-après-demain

Related

iOS Date 12/24 hour formatting (Objective C)

I need to convert a 12 Hour time format to 24 Hour format. Specifically the PM part of a date, as the API I communicate with only accepts 24-hour format.
Example:
02:00 PM needs to be converted to 14:00.
08:30 PM needs to be converted to 20:30.
I've tried several approaches and probably been close, but I can't seem to get it quite right.
For that you need to use NSDateFormatter and convert the time format to 24 Hours format.
NSString *time12Hours = #"02:00 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"hh:mm a"];
NSDate *date = [formatter dateFromString:time12Hours];
[formatter setDateFormat:#"HH:mm"];
NSString *time24Hours = [formatter stringFromDate:date];
iPhone format strings are in Unicode format. Behind the link is a table explaining what all the letters above mean so you can build your own.
You can try this website for find your format string nsdateformatter.com whit NSDateFormatter

Data sorting according to Date

I have some data which contains sold date. I want to sort them according to Week, Month, Year. For eg. If I select week then results returned should be within 7 days of current date. Same goes for Month and Year.
The relevant field from the data which is fetched from a web service looks like the following:
AuctionStartTime = "05/03/2016 09:30:00 AM"
You have use NSDateFormatter to convert the string to your date format and using NSDateFormat to get required date values and perform your task. The set date format using following code...
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:#"Your Date Format"];
Convert string to date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:dateStr];
The your date format to set your required format. For more details of date format to click now.
You shoud use a NSDateFormatter to convert the NSString date to NSDate then you can compare that to current date. After that you can easily pick the last week's, month's or year's dates.

How does NSDate seem to know it's TimeZone?

I'm somewhat confused about how these two NSDate objects seem to know which timezone they are in. I was under the impression that an NSDate object only stored a point in time and no information about the timezone.
I'm creating and logging two dates like this:
NSString* timeString = #"6:04 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
[dateFormatter setDateFormat:#"h:mm a"];
NSDate *time = [dateFormatter dateFromString:timeString];
NSDateFormatter *currentFormatter = [[NSDateFormatter alloc] init];
[currentFormatter setTimeStyle:NSDateFormatterLongStyle];
NSLog(#"%#", [currentFormatter stringFromDate:time]);
NSLog(#"%#", [currentFormatter stringFromDate:[NSDate date]]);
This produces the output:
18:04:00 GMT
11:12:36 BST
How does it know that the first date is GMT and the second is BST?
(It's British Summer Time here. Your mileage may vary in that respect)
You're right, NSDate doesn't have a time zone. Your results don't contradict that, because you're not printing the dates-- you're printing a subset of the date information produced by passing them through a date formatter. Your currentFormatter only returns time of day information, not date information. If you add this line:
[currentFormatter setDateStyle:NSDateFormatterLongStyle];
Then the results will look something like:
January 1, 2000 at 6:04:00 PM MST
September 30, 2015 at 11:14:39 AM MDT
In other words, they show up with different time zones because they're on different dates, and the time zone reflects what's in effect on that date. In my case it's currently MDT but on Jan 1 2000 it would have been MST.

Lossless conversion of NSDate to NSString and vice versa

I have an NSDate:
2015-07-13 16:04:01 +0000
and I want to convert it to an NSString to be able to store it on a server and then read it back from server and convert back to NSDate without losing any details.
I've been looking at other posts that have suggested doing it as follows:
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSString *dateString = [dateFormatter stringFromDate:date];
and vice-versa:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [dateFormatter dateFromString:message[#"date"]];
but this leads to the loss and compromise of the format:
Jul 13, 2015
How do I convert NSDate to NSString and back, eventually getting back the exact original NSDate?
You're only formatting the original NSDate with a date (not time) style that reflects the user's preferences and you're only formatting the retrieved NSDate with day, month, and year, i.e. "dd-MM-yyyy". You should be consistent your NSDateFormatters in order to maintain the original format.
Change both
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
and
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
to
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
to get and keep the entire date and time format.
An NSDate stores the number of seconds since a reference date. If you are using only iOS applications. then you call a method returning that number of seconds as an NSTimeInterval which is a number, and then you store the number. This means there is absolutely no loss of information. Your data will come back with better than microsecond precision.
If the data has to be read by different devices not running iOS then there is another method returning the number of seconds since Jan 1st, 1970. This is a very standard format that any OS should be able to handle.
Storing a number instead of a string seems to be much better.

iOS how to show a date in another language,

I've recently been working on a French version of our application use NSLocalizedStrings, so far everything is working great.
But my problem now is with dates. I show dates often in my application, in different formats, depending on the situation.
Ex:
-Fri Feb 22, 2013
-Monday February 18, 2013
-Feb 18
-Dec 5, 2012
The thing is, dates in French are not only different in terms of the name of the month, but also the order in which month, day and year appear.
Ex:
-Dec 5, 2012 would be 5 Dec 2012
-Monday February 18, 2013 would be Lundi le 18 Fevrier 2013.
I have the individual month/day names in my Localizable.string files, but how do I manage the order of how it's displayed.
Should I have an if statement that checks the current device language like so?:
NSString *currentLanguage = [[NSLocale preferredLanguages] objectAtIndex:0];
if([currentLanguage isEqualToString:#"fr"])
{
//Handle French logic
}
This is probably not the best way to go about it.
Any ideas?
You should use an NSDateFormatter and feed it your desired NSLocale like this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"nl_NL"];
dateFormatter.dateFormat = #"EEEE d MMMM yyyy HH:mm";
'EEEE' is the full name of the day of the week, which, in my case, will be displayed in dutch.
Use NSDateFormatter. For example:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:162000];
NSString *formattedDateString = [dateFormatter stringFromDate:date];
NSLog(#"formattedDateString: %#", formattedDateString);
NSDateFormatterMediumStyle will automatically format the date according to the user's preference (English, French, etc.).
If you need custom styling and the app runs in iOS 4.0+, you can use a custom template in your date formatter:
NSString *formatString = [NSDateFormatter dateFormatFromTemplate:#"EdMMMyyy" options:0
locale:[NSLocale currentLocale]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:formatString];
NSString *todayString = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"todayString: %#", todayString);
Set proper locale to your NSDateFormatter when you convert date to string, then date formatter will handle all specifics of formats depending on user settings for you:
NSDateFormatter *formatter = ... // Create and setup formatter
[formatter setLocale:[NSLocale autoupdatingCurrentLocale]];
// Now you can convert date to string
That's probaly much easier:
There is something which is called
NSDateFormatterShortStyle,
NSDateFormatterMediumStyle
NSDateFormatterLongStyle
Set Date and Time components individually:
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
Ios will format that correctly if the language of the user.
Further look at Data Formatting Guide
I would try the class function localizedStringFromDate:dateStyle:timeStyle: in NSDateFormatter before attempting to use the NSDateFormatter and a template (from Unicode Technical Standard #35):
Example:
[NSDateFormatter localizedStringFromDate:dateTime dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterNoStyle];
You can select both different date and time outputs of various lengths detailed in the docs.
Apple documentation: NSDateFormatter localizedStringFromDate:dateStyle:timeStyle:

Resources