I need to set my own custom month and DOW names for NSDateFormatter in order to parse NSDate to NSString. Is it possible to set array with custom names to the date formatter?
Look at the docs for NSDateFormatter. Use the setMonthSymbols: or setShortMonthSymbols: methods (and other similar methods as needed).
This will work for converting an NSDate to an NSString giving you whatever custom month names and day-of-week names you want in the result.
Related
I'm trying to display the month and day of a given date, like so:
"November 5"
but allow it to change the order based on locale.
"5 November" (or whatever other people do)
I am aware that I can simply hard code the formats like so:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMMM d"];
But, this doesn't update for locale, and I don't want to have to hard code a list of locales that use one style or the other.
Generally when I want to display a date for a given local, I know that I can use setDateType on the formatter, and pick from a number of pre-existing formats that will nicely account for the current locale. Unfortunately, none of the existing NSDateFormatterStyle will display the way I need them to.
The one solution I have been able to think of is to set the date style to long style, and then read through the dateFormat string, and see if I hit a 'd' or 'M' first. Then format it accordingly. This would be fairly easy to do, but it seems really hacky. Surely there is a better way to do that.
Any suggestions?
Apparently NSDateFormatter dateFormatFromTemplate is a thing, and it solves my problem.
NSString *format = [NSDateFormatter dateFormatFromTemplate:#"MMMM d" options:0 locale:[NSLocale currentLocale]];
This will format the date correctly following your template according to current locale.
While querying data from a database I receive the hours a process is started and ended in two separate string fields for example start = "1100" and end = "+0200" which indicate it's hours of operation are from 11am-2am. What is the proper way to represent this in swift so that I can determine the amount of time left from the current time to the end time of the process.
EDIT:
I found an interesting way using the date formatter if I remove any possible prefix of + and use the below code it seems to work correctly; however, the date is not set is their a work around?
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "HHmm"
let date = dateFormatter.dateFromString("1340")
You can create NSDate from components using NSCalendar.currentCalendar().dateWithEra (there are other similar functions, look up NSCalendar for details). You will need to add some logic to determine if the 2AM is today or tomorrow etc.
Then you can compare two NSDate dates. To determine time left to the end you would probably use NSDate method timeIntervalSinceDate. You can also use NSDateComponentsFormatter to get the remaining time nicely formatted.
I have dates in format e.g.
2014-12-09T11:10:23.0000000-08:00
How To parse this date & get 2014-12-09T11:10:23.
Please Help I have tried with many date formatters in setDateFormat: method.
setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZZZZZ"
(It's all in the spec if you bother to look.)
Before flagging this question as a duplicate, please read on.
I need to compare two NSDates. A date I get from a server with the current date.
From NSDate.date() I get this date 2014-09-25 12:48:23 +0000 which is wrong (the time part). I needed to add 5 hours to get the current time so I did the following.
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd hh:mm:ss ZZZZZ"
let dateString = formatter.stringFromDate(NSDate.date())
The result is the correct date - 2014-09-25 06:21:56 +05:30
But there's a little hitch. This date is a String, not a NSDate. I need it to be a NSDate object to compare it with another date.
I tried converting it back like this,
let date = formatter.dateFromString(dateString)
And I get a wrong result - 2014-09-25 00:55:53 +0000. I tried passing the date string to a new NSDateFormatter to see if that works but again I still I get the wrong date.
My question is, how can I convert this date string to a NSDate object which also retains the correct time.
Thank you.
You are thoroughly confused about NSDate.
An NSDate is a point in time. It has no time zone information. If we both call [NSDate date] right now, we will get the same NSDate, even when you are in India and I'm in the UK. That's intentional. It's the same time. The time displayed on my watch and on your watch is different, but NSDate is the same. You can't convert NSDate to an "Indian" date.
You use calendars and timezones to convert NSDates to strings that you display to a user, in the way your users expect it. That's what you have done. You got a string that makes sense to Indian users. If an Indian user types a time, you take that string and convert it to an NSDate. The NSDate will be in Universal time. If you and I both typed in the time on our watch right now and converted it, you would type a time that looks like 5 1/2 hours earlier than mine. But it's the same time. If you convert it to NSDate, we will both get the exact same NSDate.
So how do you change your NSDate? Quite simple: You don't. NSDate is absolute time, independent of your location on earth.
Upon further Googling, I came across this post. The method described in it does exactly what I want. The original code is in Objective-C and since my question is in Swift, I'm going to post its Swift translation. \
func toLocalTime() -> NSDate {
let timeZone = NSTimeZone.localTimeZone()
let seconds = timeZone.secondsFromGMTForDate(self)
return NSDate(timeInterval: Double(seconds), sinceDate: self)
}
I added these as extension methods of NSDate so you can simply call them like this.
NSDate.date().toLocalTime()
You can compare two dates using any of the following NSDate functions: compare, earlierDate, laterDate, isEqualToDate. You should not compare date strings (oh, goodness, no, think of the nightmare); convert 'date strings' into 'NSDate' as soon as inputed.
In order to compare two dates that arose from strings correctly, you'll need the date strings to be unambiguous. In practice, that requires the date to have a time zone attached. If your server isn't providing a time zone and can't be modified to provide one, then you'll be forced to assume one (which would typically be the time zone where the server is located, assuming one server).
How would you get the substring of a string that is dynamic? By this I mean that I am retrieving tweets and I want to extract different parts of the return value into a Month String, a Year int, a day int, and a time int. Here is what I am retrieving:
Sun Nov 17 00:15:47 +0000
How would I split it up even if the given value would change over time, as the tweet is on a index path of multiple table view cells, and the value of the creation date would be different for each one of the cells?
Your string represents a date, so the first thing you should do is convert it to an NSDate using an NSDateFormatter.
Then you can get the various elements of the date by creating an NSDateComponents instance via -[NSCalendar components:fromDate:], and accessing its month, day, and whatever other properties you need.