Swift. Set specific time format(12/24h) for custom Locale - ios

I want to get the time in 12h or 24h format from DateFormatter according to option "24-Hour-Time" in User Preferences.
If I set "24-Hour-Time" = "on" and DateFormatter like this:
let formatter = DateFormatter()
let locale = Locale(identifier: "en_US")
print(locale) // "en_US (fixed)\n"
formatter.locale = locale
formatter.dateStyle = .none
formatter.timeStyle = .short
formatter.string(from: Date()) // "2:54 PM" (not "14:54")
But if I set:
let locale = Locale.current // "en_US (current)\n"
instead of
let locale = Locale(identifier: "en_US") // "en_US (fixed)\n"
the result will be
formatter.string(from: Date()) // "14:54"
How to create custom locale setting 12 or 24 hour format.

//MARK:- use this to convert 24 hours time format to 12 hour Formate String
func TwelveHourFormateFrom24Hours(time : String)->String
{
let Time = time.components(separatedBy: ".")
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "H:mm:ss"
if let inDate = dateFormatter.date(from: Time[0])
{
dateFormatter.dateFormat = "h:mm a"
dateFormatter.timeZone = TimeZone.current
let outTime = dateFormatter.string(from: inDate)
print("in \(time)")
print("out \(outTime)")
return outTime
}
return time
}
//MARK:- 24 hour Formate String
func TwentyFourHourFormate(time : String)->String
{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "h:mm a"
if let inDate = dateFormatter.date(from: time)
{
dateFormatter.dateFormat = "H:mm:ss.sss"
let outTime = dateFormatter.string(from: inDate)
print("in \(time)")
print("out \(outTime)")
return outTime
}
return time
}

Related

Date format in Swift ios

how i can implement this date format 1st July,2021 17:00.My current code is
func changeFormat(_ toFormat: String,_ dateStr: Date?) -> String{
let date = dateStr ?? Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = toFormat
return dateFormatter.string(from: date)
}
func covertStringToDate(_ date: String) -> Date {
let isoDate = date
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX
dateFormatter.dateFormat = "yyyy-MM-dd"
return dateFormatter.date(from:isoDate)!
}
If u want to convert any Date object to this format do this.
let date = Date()
//create the date format without the day
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "MMMM,yyyy HH:mm"
let dateWithoutDay = dateFormatter.string(from: date)
//get the day from the date
let dayFormatter = DateFormatter()
dayFormatter.locale = Locale(identifier: "en_US_POSIX")
dayFormatter.dateFormat = "d"
let day = dayFormatter.string(from: date)
//create a number formatter to get st/nd/rd/th suffix
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .ordinal
let dayWithSuffix = numberFormatter.string(from: NSNumber(value:Int(day)!))
print("\(dayWithSuffix!) \(dateWithoutDay)") // --> 3rd September,2022 22:52
If u want to convert this type of date (1st July,2021 17:00 ) to a Date object do this.
let dateString = "1st July,2021 17:00"
let dateSuffix = dateString.components(separatedBy: CharacterSet.decimalDigits).joined().prefix(2)
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "dd'\(dateSuffix)' MMMM,yyyy HH:mm"
let date = dateFormatter.date(from: "1st July,2021 17:00")
print(date!) // ->> 2021-07-01 11:30:00 +0000

Swfit- Check whether current time is between two time string

How to check if the current time is between two-time strings in the following format.
HH:MM AM/PM
say if
startTime = "10:30 AM"
endTime = "06:30 PM"
how to check if currentTime() value is in between start and end times?
You can set the date formatter defaultDate for today, parse the date strings, create a DateInterval with the start and end date and check if it contains now Date():
extension Formatter {
static let today: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.locale = .init(identifier: "en_US_POSIX")
dateFormatter.defaultDate = Calendar.current.startOfDay(for: Date())
dateFormatter.dateFormat = "hh:mm a"
return dateFormatter
}()
}
func checkIfCurrentTimeIsBetween(startTime: String, endTime: String) -> Bool {
guard let start = Formatter.today.date(from: startTime),
let end = Formatter.today.date(from: endTime) else {
return false
}
return DateInterval(start: start, end: end).contains(Date())
}
let startTime = "10:30 AM"
let endTime = "06:30 PM"
checkIfCurrentTimeIsBetween(startTime: startTime, endTime: endTime) // true
You can use Swift's Dateformatter() to encode and compare dates:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "hh:mm a"
dateFormatter.locale = Locale.init(identifier: "en_GB")
dateFormatter.defaultDate = Calendar.current.startOfDay(for: Date())
dateFormatter.amSymbol = "AM"
dateFormatter.pmSymbol = "PM"
let startTimeFormatted = dateFormatter.today.date(from: startTime)
let endTimeFormatted = dateFormatter.today.date(from: endTime)
//Now you can compare the two, e.g.
if startTimeFormatted > currentDate() && currentDate() < endTimeFormatted{
return true
}

Swift datefomatter returns wrong time

I am working in iOS app with Dateformatter which is return wrong time. I don't have any idea to fix this. Can anyone please correct me to get correct time?
let dateString = "2020-08-11T05:32:33.000Z"
func approach1(){
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX"
guard let date = dateFormatter.date(from: dateString) else {fatalError()}
printTime(date: date)
}
func approach2(){
let dateFormatter = ISO8601DateFormatter()
dateFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
guard let date = dateFormatter.date(from: dateString) else { fatalError()}
printTime(date: date)
}
func printTime(date: Date){
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "h:mm a"
dateFormatter.amSymbol = "AM"
dateFormatter.pmSymbol = "PM"
let time = dateFormatter.string(from: date)
print("date: \(date)")
print("Time: \(time)") // Here it's always wrong time with those approach.
}
Current Result:
By default dateFormatter works with local time zone. To have correct display set its time zone to UTC : dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
There's nothing wrong. The date formatter returns the correct time
The date string
"2020-08-11T05:32:33.000Z"
represents a date in UTC(+0000).
Your local time zone is obviously UTC+0530. DateFormatter considers the local time zone by default.
To format the time also in UTC you have to set the time zone of the date formatter explicitly
func printTime(date: Date) {
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(identifier: "UTC")
dateFormatter.dateFormat = "h:mm a"
dateFormatter.amSymbol = "AM"
dateFormatter.pmSymbol = "PM"
let time = dateFormatter.string(from: date)
print("date: \(date)")
print("Time: \(time)")
}
You need to specify the time zone when formatting to string (by default it's using your current local time zone):
func printTime(date: Date) {
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "UTC") // <- add here
dateFormatter.dateFormat = "h:mm a"
dateFormatter.amSymbol = "AM"
dateFormatter.pmSymbol = "PM"
let time = dateFormatter.string(from: date)
print("date: \(date)")
print("Time: \(time)")
}

Date from String is not giving correct date

I want to set "EST" as the default time zone for every user, but there is one condition that needs to be check in the current date at 7:45 PM. So I am comparing two dates, but the problem is when I convert the current Date to String it gives me the correct EST time, when I convert that String again to Date in EST it gives me time 4 hours ahead of EST. Here is the code for conversion
class func getCurrentDateTime() -> String {
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "EST")
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
let dateString = dateFormatter.string(from: Date())
print(dateString)
let convertDateFormatter = DateFormatter()
convertDateFormatter.dateFormat = "dd-MM-yyyy"
convertDateFormatter.timeZone = TimeZone(abbreviation: "EST")
let currentDate = convertDateFormatter.string(from: Date())
print(currentDate)
let comparedDateFormatter = DateFormatter()
comparedDateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
comparedDateFormatter.timeZone = TimeZone(abbreviation: "EST")
let comparedDate = comparedDateFormatter.date(from: "\(currentDate) 19:45:00")
print(comparedDate)
let currentDateFormatter = DateFormatter()
currentDateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
comparedDateFormatter.timeZone = TimeZone.init(abbreviation: "EST")
let currentDateAndTime = currentDateFormatter.date(from: dateString)
print(currentDateAndTime)
return dateString
}
So a date does not have a time zone.
So when I use a dateFormatter to convert a date to a string representation of the string will reflect the time zone the dateFormatter is set to.
But when you use the same formatter to convert the the string back into a date the date would not have the the time zone offset anymore.
So this sounds to me as if it is working properly.
Edit:
So if you are trying to compare two dates I would do something like:
let date1 = Date()
let date2 = Date().addingTimeInterval(100)
if date1 == date2 {
// dates are exactly equal
} else if date1 > date2 {
// date1 is the most recent
} else if date1 < date2 {
// date2 is the most recent
}
And if I were trying to display these dates I would use the date formatter to convert them to strings.
I modified the code as you requested:
func getCurrentDateTime() -> String {
var checkString : String!
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "EST")
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss a"
let dateString:String! = dateFormatter.string(from: Date())
let dateFormatter1 = DateFormatter()
dateFormatter1.timeZone = TimeZone(abbreviation: "EST")
dateFormatter1.dateFormat = "dd-MM-yyyy HH:mm:ss"
let dateString1:String! = dateFormatter1.string(from: Date())
let convertDateFormatter = DateFormatter()
convertDateFormatter.dateFormat = "dd-MM-yyyy"
convertDateFormatter.timeZone = TimeZone(abbreviation: "EST")
let currentDateformat = convertDateFormatter.string(from: Date())
let compareDate = "\(currentDateformat) 07:45:00 PM"
let compareDate1 = "\(currentDateformat) 07:45:00"
if dateString == compareDate {
checkString = "equal date"
}
if dateString1 < compareDate1{
checkString = "greater than"
} else {
checkString = "less than"
}
return checkString
}
func getCurrentDateTime() -> String {
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "EST")
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
let dateString = dateFormatter.string(from: Date())
let convertDateFormatter = DateFormatter()
convertDateFormatter.dateFormat = "dd-MM-yyyy"
convertDateFormatter.timeZone = TimeZone(abbreviation: "EST")
let currentDate = convertDateFormatter.string(from: Date())
let comparedDateFormatter = DateFormatter()
comparedDateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
let comparedDate = comparedDateFormatter.date(from: "\(currentDate) 19:45:00")
let currentDateFormatter = DateFormatter()
currentDateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
let currentDateAndTime = currentDateFormatter.date(from: dateString)
return dateString
}
Plesae check this code .
Actually I didn't find anything wrong with the code.
The Date always be in UTC timezone. The DateFormatter did the magic.
To print Date in as per the format:
use string(from:) method.
if let currentDateAndTime = currentDateFormatter.date(from: dateString) {
print(currentDateFormatter.string(from: currentDateAndTime))
}
NB: When working with fixed format dates, such as RFC 3339, you set
the dateFormat property to specify a format string. For most fixed
formats, you should also set the locale property to a POSIX locale
("en_US_POSIX"), and set the timeZone property to UTC.

Convert date string swift

I have a date string in this format:
2016-06-20T13:01:46.457+02:00
and I need to change it to something like this:
20/06/2016
Previously I have always used this library -> SwiftDate to manipulate the dates, but it doesn't work now.
I tried also something like:
let myDate = self.dateNoteDict[indexPath.row]!
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss.SSSSxxx"
let date = dateFormatter.dateFromString(myDate)
print("date -> \(date)")
but it doesn't work. How can I do?
Thanks in advance.
The standard ISO8601 date format with fractional seconds and time zone is
yyyy-MM-dd'T'HH:mm:ss.SSSZ
let myDate = "2016-06-20T13:01:46.457+02:00"
let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") // edited
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date = dateFormatter.dateFromString(myDate)!
dateFormatter.dateFormat = "dd/MM/yyyy"
let dateString = dateFormatter.stringFromDate(date)
Swift 3:
let myDate = "2016-06-20T13:01:46.457+02:00"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX") // edited
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date = dateFormatter.date(from:myDate)!
dateFormatter.dateFormat = "dd/MM/yyyy"
let dateString = dateFormatter.string(from:date)
In macOS 10.13+, iOS 11+ ISO8601DateFormatter is an alternative as input formatter
let myDate = "2016-06-20T13:01:46.457+02:00"
let inputFormatter = ISO8601DateFormatter()
inputFormatter.formatOptions = [.withFullDate, .withFullTime, .withFractionalSeconds]
let date = dateFormatter.date(from:myDate)!
let outputFormatter = DateFormatter()
outputFormatter.locale = Locale(identifier: "en_US_POSIX")
outputFormatter.dateFormat = "dd/MM/yyyy"
let dateString = dateFormatter.string(from:date)
I always use this code while converting String to Date .
(Swift 3)
extension String
{
func toDate( dateFormat format : String) -> Date
{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = format
if let date = dateFormatter.date(from: self)
{
return date
}
print("Invalid arguments ! Returning Current Date . ")
return Date()
}
}
and just call like . .
print ( "2016-06-20T13:01:46.457+02:00".toDate(dateFormat: "yyyy-MM-dd'T'HH:mm:ss.SSSZ") )
//Capital HH for 24-Hour Time
I always use this code while converting String to Date. (Swift 4.2)
let myDate = datePicker.date
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date = myDate
dateFormatter.dateFormat = "dd/MM/yyyy"
let dateString = dateFormatter.string(from:date)
print(dateString)
You can try manually also:
let myDate = "2019-02-22T08:21:11+0000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date = myDate
dateFormatter.dateFormat = "dd/MM/yyyy"
let dateString = dateFormatter.string(from:date)
print(dateString)

Resources