This question already has answers here:
NSDateFormatter and current language in iOS11
(5 answers)
Closed 3 years ago.
When using a DateComponentFormatter like this
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .full
formatter.includesApproximationPhrase = false
formatter.includesTimeRemainingPhrase = false
formatter.allowedUnits = [.hour]
var components = DateComponents()
components.hour = 4
let outputString = formatter.string(from: components)
The output is '4 hours' when my language/region is English/US. When changing the language/region to French/France I had expected the output to be '4 heures`, but it still comes out as '4 hours'. Any suggestions on how to get the DateComponentFormatter to do translations?
Changing the language/region on the phone seems to have no effect.
May be, Your app is not localized to other languages. I followed https://www.raywenderlich.com/250-internationalizing-your-ios-app-getting-started for adding localization in my app.
Per above, the underlying reason is that Apple changed the behavior of the formatters in iOS11 and I hadn't caught that. This question was answered by NSDateFormatter and current language in iOS11
Related
This question already has answers here:
Getting the difference between two Dates (months/days/hours/minutes/seconds) in Swift
(20 answers)
Closed 5 years ago.
currentCalendar.dateComponents(components: Set Calendar.Component, from: Date, to: Date)
I want to calculate the number of days between 2 dates, I can't seem to find a tutorial anyway online for swift4 Xcode9 everything else seems to be about the outdated syntax. so I just want to know what should I put in the "components: Set Calendar.Component" part ?
You should put a Set of the enumeration type Calendar.Component
example:
let s: Set = [Calendar.Component.day]
let c = Calendar(identifier: .gregorian)
let dc = c.dateComponents(s, from: Date.init(timeIntervalSinceNow: -100000), to: Date.init(timeIntervalSinceNow: 0))
let day = dc.day
the day variable is equal to 1
This question already has answers here:
Convert NSDate to NSString
(19 answers)
Closed 5 years ago.
Can anyone please help me converting current date to this format:
"2017-06-15T05:15:31.158Z".
I need this format only, another format is not acceptable to my backend.
Friend, please help me here.
try this
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let timeString = formatter.stringFromDate(NSDate())
print(timeString)
How to get yyyy-mm-dd hh:mm:ss format from "2015-06-26T00:10:00+01:00" in swift
This question already has answers here:
NSDate of yesterday
(6 answers)
How to add minutes to current time in swift
(11 answers)
Closed 5 years ago.
I use this code to get the current date
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy.MM.dd"
let result = formatter.string(from: date)
Any idea on how can I get the date in the past, for example a day or a year before the current date?
Try to use this:
Date(timeIntervalSinceNow: -3600); \\One hour
Or you can use Calendar for this
let calendar = Calendar.current
var component = DateComponents()
component.year = -1
calendar.date(byAdding: component, to: Date())
This question already has answers here:
Getting the difference between two Dates (months/days/hours/minutes/seconds) in Swift
(20 answers)
Closed 6 years ago.
I'm playing with swift. I want to show how many years/months/days has been passed since the specific date(NSdate).
If the specific date is 08/01/2016 and current date is 08/11/2016, it will show only "10day(s) has been passed". If the specific date is 08/11/2015, it will show "1year(s) 0 month(s) 0day(s)has been passed"
Do something like this. Use your dates in date1 and date2
let date1 = NSDate()
let date2 = NSDate()
let form = NSDateComponentsFormatter()
form.maximumUnitCount = 2
form.unitsStyle = .Full
form.allowedUnits = [.Year, .Month, .Day]
let s = form.stringFromDate(date1, toDate: date2)
Basically what I want is to get the value of a time interval represented in hours only, without rounding it to full hours (using NSDateComponentsFormatter to get it properly formatted and localized). I don't know if I misunderstand the use of NSDateComponentsFormatter.allowsFractionalUnits, but I can't get the formatter to give me a decimal value. Can anyone help me spot my error or tell me in what way I misunderstand this?
From Apple docs about allowsFractionalUnits property:
Fractional units may be used when a value cannot be exactly
represented using the available units. For example, if minutes are not
allowed, the value “1h 30m” could be formatted as “1.5h”.
Swift example code:
let formatter = NSDateComponentsFormatter()
formatter.unitsStyle = .Abbreviated
formatter.allowedUnits = .Hour
formatter.allowsFractionalUnits = true
let onePointFiveHoursInSeconds = NSTimeInterval(1.5 * 60.0 * 60.0)
print(formatter.stringFromTimeInterval(onePointFiveHoursInSeconds)!)
//"1h" instead of expected "1.5h"
Same example in Objective-C code:
NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
formatter.unitsStyle = NSDateComponentsFormatterUnitsStyleAbbreviated;
formatter.allowedUnits = NSCalendarUnitHour;
formatter.allowsFractionalUnits = YES;
NSTimeInterval onePointFiveHoursInSeconds = 1.5 * 60.0 * 60.0;
NSLog(#"%#", [formatter stringFromTimeInterval:onePointFiveHoursInSeconds]);
//"1h" instead of expected "1.5h"
Update:
I have reported a bug to Apple about this problem (rdar://22660145).
According to Open Radar #32024200:
After doing some digging (disassembling Foundation), it looks like every call to -[_unitFormatter stringFromNumber:] in -[NSDateComponentsFormatter _stringFromDateComponents:] is passed an +[NSNumber numberWithInteger:] which drops floating point data.
You're not doing anything wrong. The flag is simply broken.
Looking at the documentation, it’s using a lot of interesting language (emphasis mine):
Fractional units may be used when a value cannot be exactly represented using the available units. For example, if minutes are not allowed, the value “1h 30m” could be formatted as “1.5h”.
While to me it seems that it would only make sense for the values in the documentation to be values that actually work, it’s certainly possible that there is some combination of time value, formatter options, and calendar/locale settings that would make this work. Definitely worth filing a Radar on both the functionality and the documentation.
enter code hereAs far as i understood , You want to display time in 12-Hour formate Right ?
Below is the code :
Swift->
let dateAsString = "20:30"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "HH:mm"
let date = dateFormatter.dateFromString(dateAsString)
dateFormatter.dateFormat = "h:mm a"
let date12 = dateFormatter.stringFromDate(date!)
txtText.text = date12