How to show the Netherlands time in hours (uur) in Dutch? - ios

Show time for multiple countries with native time format.
Trying to Display time of Netherland eg: 22.37 uur
With Locale: nl_NL
For Date: 2019-09-17 17:07:00 +0000
I have tried the code below:
let formatter = DateComponentsFormatter()
var calendar = Calendar.gregorianCalendar()
calendar.locale = apiDate.locale // nl_NL
formatter.calendar = calendar
formatter.unitsStyle = .short
formatter.allowedUnits = [.hour, .minute]
formatter.zeroFormattingBehavior = [.dropLeading, .pad]
let dateComponent = calendar.dateComponents([.hour, .minute], from: apiDate.absoluteDate) //apiDate.absoluteDate: Date : 2019-09-17 17:07:00 +0000
print(formatter.string(from: dateComponent) as Any) // prints 22 uur, 37 min
Expected: 22.37 uur
Actual: 22 uur, 37 min

Related

Convert date with timezone

I am getting date from server: 2022-12-14 20:59:59 +0000
I have view with promo codes, so according to received date I should show after how many seconds it will be expired.
For example if my current date is 2022-12-14 19:59:59 +0000, I must show 60:00 seconds.
If the users timezone is GMT+4, he must see 60:00 seconds as well.
So I use this code:
let currentTime = Int(Date().timeIntervalSince1970)
let expirationDate = model.expirationDate
expiredText = expirationDate.dateStringWithFormat("dd.MM.yyyy")
timeLeft = Int(expirationDate.timeIntervalSince1970) - currentTime
Result is:
currentTime: 2022-12-14 13:48:16 +0000 // my current time is 16:48:16
expirationDate: 2022-12-14 20:59:59 +0000
timeLeft: "0d. 07:11:43"
So Date() returns wrong date.
I tried to use calendar, but result is the same.
let calendar = Calendar(identifier: .gregorian)
let currentDate = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss:Z"
dateFormatter.timeZone = calendar.timeZone
How can I correctly convert my timezone to timezone of received date or vice versa?
Thanks
I think you are making this more complicated than it is.
We have a server timestamp
let serverDate = "2022-12-14 20:59:59 +0000"
that we convert to a Date using a formatter
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ssZ"
Then we want to calculate the difference between the server data and now
if let expirationDate = dateFormatter.date(from: serverDate) {
let components = Calendar.current.dateComponents([.day, .hour, .minute, .second], from: .now, to: expirationDate)
}
and then we can use a DateComponentsFormatter to get the difference as a string
let compFormatter = DateComponentsFormatter()
print(compFormatter.string(from: components))

Converting the string to date giving different format [duplicate]

This question already has answers here:
Getting date from [NSDate date] off by a few hours
(3 answers)
Closed 3 years ago.
Converting from string to date and date to string time format is changing the original data.
Tried with dateComponents as well by giving the hour and minute
var calendar = Calendar.current
let components = calendar.dateComponents([.year, .month, .day, .hour], from: calFrom)
calendar.timeZone = .current
// Specify date components
var dateComponents:DateComponents = calendar.dateComponents([.year, .month, .day, .hour], from: Date())
dateComponents.year = components.year
dateComponents.month = components.month
dateComponents.day = components.day
dateComponents.hour = 08//Cutomised hour
dateComponents.minute = 34//Cutomised Minutes
// Create date from components
let someDateTime = calendar.date(from: dateComponents)
print(someDateTime!)
Actual Output:
2019-04-02 03:04:00 +0000
Expected Output:
2019-04-02 08:34:00 +0000
I tried with below code as well. Converting the date to String and manually appending the hour and minutes to the string and converting back to the date.
let calFrom = Date()
let formatter = DateFormatter()
formatter.dateFormat = "dd/MM/yyyy"
var calFromDate = formatter.string(from: calFrom)
calFromDate = calFromDate + " 09" + ":30"
print(calFromDate)
//Output 02/04/2019 09:30
formatter.dateFormat = "dd/MM/yyyy hh:mm"
formatter.locale = Locale.current// set locale to reliable US_POSIX
let date1 = formatter.date(from: calFromDate)
print(date1!)
Actual Output:
2019-04-02 04:00:00 +0000
Expected Output:
02/04/2019 09:30
How to get the exact time that has given in the output?
Date used to update the hour and minute components has UTC timezone so calendar should also have the same timeZone as below,
calendar.timeZone = TimeZone(abbreviation: "UTC")!

Swift4: Calendar gives me wrong value for secondsFromGMT

For my iOS App I need some calculations for Dates and Time. By using the class Calendar I found out, that this class gives me some wrong values for dates in the 19th century and before. During this time, the time zones in Europe were very unstructured. For example, GMT+0:53:28. The class Calendar knows about this and shows me the correct time zone. But the return value of "secondsFromGMT" is wrong: 3600 seconds in this case. Does anyone have some experience with this error or with related problems?
import Foundation
let timeZone = TimeZone(identifier: "Europe/Berlin")
var calendar = Calendar.current
calendar.timeZone = timeZone!
let formatter = DateFormatter()
formatter.dateFormat = "dd.MM.yyyy HH:mm:ss"
formatter.locale = Locale(identifier: "en-US")
let date = formatter.date(from: "01.07.1622 08:10:00")!
formatter.dateStyle = .long
formatter.timeStyle = .long
let stringDate = formatter.string(from: date)
let offsetFromGMT = calendar.timeZone.secondsFromGMT(for: date)
print(stringDate, " Offset =", offsetFromGMT)
July 1, 1622 at 8:10:00 AM GMT+0:53:28 Offset = 3600

Setting particular Interval of time for DatePicker (Minimum and Maximum Time) Swift 3

Hi Im trying to limit my date Picker values within a particular Period of time as per my Backend Response. I refer many Links but nothing helps me, here is my code.
datePicker.datePickerMode = .time
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let today = dateFormatter.string(from: Date())
let preprTime = UserDefaults.standard.string(forKey: "PREPARATION_TIME")!
let start = UserDefaults.standard.string(forKey: "START_TIME")!
let end = UserDefaults.standard.string(forKey: "END_TIME")!
//Prints--> preprTime = 30 Mins., start = 08:00:00, end = 23:00:00
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss z"
let startDt = dateFormatter.date(from: "\(today) \(start) z")
let endDt = dateFormatter.date(from: "\(today) \(end) z")
//Prints--> startDt = 2017-11-02 08:00:00 +0000, endDt = 2017-11-02 23:00:00 +0000
let mins = preprTime.trimmingCharacters(in: CharacterSet(charactersIn: "01234567890").inverted)
let startTime: Date = startDt!.addingTimeInterval(TimeInterval(Int(mins)! * 60))
let endTime: Date = endDt!.addingTimeInterval(-(TimeInterval(Int(mins)! * 60)))
//Prints--> startTime = 2017-11-02 08:30:00 +0000, endTime = 2017-11-02 22:30:00 +0000
datePicker.minimumDate = startTime
datePicker.maximumDate = endTime
//Prints--> datePicker.minimumDate = 2017-11-02 08:30:00 +0000, datePicker.maximumDate = 2017-11-02 22:30:00 +0000
I Think I almost done, it prints the correct values, but the datePicker's Minimum and maximum dates are not set properly. Which means the datePicker's minimumDate(time) is today 2.00 PM and maximumDate(time) is today 11.59 PM. I dont know where im doing mistake, can someone help me to trigger this...?
Thanks in Advance....!
Try adjusting your mimimumDate and maximumDate based on the difference in timezone
The difference is
let gmtDiff = TimeZone.current.secondsFromGMT()
My timezone is +5:30, so I need to subtract it from start and end time
var startDt = dateFormatter.date(from: "\(today) \(start) z")
startDt = startDt?.addingTimeInterval( -TimeInterval(gmtDiff))
var endDt = dateFormatter.date(from: "\(today) \(end) z")
endDt = endDt?.addingTimeInterval( -TimeInterval(gmtDiff))

how to get NSDate of a specific next day and time

I have some events for which I need to calculate NSDates.
For example I'm trying to get the next Monday at 8:00 AM.
So I tried some stuff but nothing works:
1.
let nextMonday = NSCalendar.currentCalendar().dateBySettingUnit(NSCalendarUnit.Weekday, value: 2, ofDate: startDate, options: NSCalendarOptions.MatchNextTime)
let nextMondayEight = NSCalendar.currentCalendar().dateBySettingUnit(NSCalendarUnit.Hour, value: 8, ofDate: nextMonday!, options: NSCalendarOptions.MatchNextTime)
I get:
2016-04-12 05:00:00 +0000
That's Tuesday at 8:00 (the time difference is my local time GMT -3).
2.
let unitFlags: NSCalendarUnit = [.Day, .Month, .Year]
let comp = NSCalendar.currentCalendar().components(unitFlags, fromDate: NSDate())
comp.timeZone = NSTimeZone.localTimeZone()
comp.weekday = 1
comp.hour = 8
comp.minute = 0
comp.second = 0
let compDate = NSCalendar.currentCalendar().dateFromComponents(comp)
print("time: \(compDate!)")
I get:
2016-04-11 05:00:00 +0000
That's today at 8:00 and not next Monday at 8:00.
Any suggestions?
Thanks
NSCalendar has a method nextDateAfterDate:matchingComponents:options for this kind of date math.
let calendar = NSCalendar.currentCalendar()
let components = NSDateComponents()
components.hour = 8 // 8:00
components.weekday = 2 // Monday in Gregorian Calendar
let nextMondayEightOClock = calendar.nextDateAfterDate(NSDate(), matchingComponents: components, options: .MatchStrictly)

Resources