Date format in Swift ios - 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

Related

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

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
}

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.

How to convert "2017-07-11T06:52:15.948Z" in to like "JUL, 7 2017" in swift

I'm trying to convert string to date, and again date to string.
I tried below, but it gives me a nil value error
let string = "2017-07-11T06:52:15.948Z"
let dateFormatter = DateFormatter()
let tempLocale = dateFormatter.locale // save locale temporarily
dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let date = dateFormatter.date(from: string)!
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
dateFormatter.locale = tempLocale // reset the locale
let dateString = dateFormatter.string(from: date)
print("EXACT_DATE : \(dateString)")
then I tried to use only the date without time by seperating the exact string like below
let string = "2017-07-11T06:52:15.948Z"
let dateonly = (string.components(separatedBy: "T"))[0]
let dateformatter = DateFormatter()
dateformatter.dateFormat = "yyyy-mm-dd"
dateformatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") as Locale!
let date = dateformatter.date(from: dateonly!)
print(date ?? "no date ")
dateformatter.dateFormat = "MMM d, yyyy" //Your New Date format as per requirement change it own
let newDate = dateformatter.string(from: date!) //pass Date here
print(newDate)
this works fine. but show me the wrong month
hope your help with this
for reference purpose I taken the answer from here for append the milliseconds SSS
just change the date format
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
to
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
for full code
let string = "2017-07-11T06:52:15.948Z"
let dateFormatter = DateFormatter()
let tempLocale = dateFormatter.locale // save locale temporarily
dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date = dateFormatter.date(from: string)!
dateFormatter.dateFormat = "MMM d, yyyy" ; //"dd-MM-yyyy HH:mm:ss"
dateFormatter.locale = tempLocale // reset the locale --> but no need here
let dateString = dateFormatter.string(from: date)
print("EXACT_DATE : \(dateString)")
output

Converting String to Date using dd-MM-yyyy get nil in Xcode 8 in Swift 3.0

if I choose date like let dateString = "2014-01-12", How would I convert string to
dateFormatter.dateFormat = "yyyy-MM-dd"
and then in below format
dateFormatter.dateFormat = "dd-MM-yyyy"
I get nil values in Xcode 8 in Swift 3.0
{
let dateString = "2014-01-12"
dateFormatter.locale = Locale(identifier:"ja_JP")
dateFormatter.dateFormat = "dd-MM-yyyy"
print(dateFormatter.date(from:dateString))
}
or
let dateString = "2014-01-12"
dateFormatter.dateFormat = "yyyy-MM-dd"
var s = dateFormatter.date(from: dateString)! as Date
dateFormatter.dateFormat = "dd-MM-yyyy"
s = dateFormatter.date(from: dateString)! as Date
print("SecondDate\(s)")
In Xcode 7 it works fine.
If you want to convert to date formate yyyy-MM-dd to dd-MM-yyyy then your last line should be.
let dateString = "2014-01-12"
dateFormatter.dateFormat = "yyyy-MM-dd"
var s = dateFormatter.date(from: dateString)
dateFormatter.dateFormat = "dd-MM-yyyy"
Get formatted date like this
let dateStr = dateFormatter.string(from: s)
Instead of
s = dateFormatter.date(from: dateString)! as Date
You need to just change format of DateFormatter and get it as the String.
Need to change date string into "date string" as Date then you can change into your particular format. For every format you need to do this first.
You can use the code below.
Swift 3
extension String {
func changeDate(_ mydate:String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = DateFormatter.Style.long
dateFormatter.dateFormat = "yyyy-MM-dd"
let convertedDate = dateFormatter.date(from: mydate)
dateFormatter.dateFormat = "dd-MM-yyyy"
let date = dateFormatter.string(from: convertedDate!)
return date
}
}
You can use this
let dateString = "2014-01-12"
let dateFormatterNew = DateFormatter()
dateFormatterNew.dateFormat = "yyyy-MM-dd"
let date = dateFormatterNew.date(from: dateString)! as Date
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
let dateNew = dateFormatter.string(from: date )
print(dateNew)
do it like this
let datestring = "2014-01-12"
let dateF = NSDateFormatter()
dateF.dateFormat = "yyyy-MM-dd"
let firstDate = dateF.dateFromString(datestring)
dateF.dateFormat = "dd-MM-yyyy"
let newDateString = dateF.stringFromDate(firstDate!)
print("NEW DATE FORMAT IS : \(newDateString)")

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