How to compare two different date formats in swift - swift

I have two different date formats from the API for the same object one is "yyyy-MM-dd" and other one is "dd-MM-YYYY" how to differentiate, that object contains specific format.

Use this may be worked for you.
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd" // YOUR DATE FORMATE
if let date = formatter.date(from: dateString) {
// IT CONTAIN YOUR DATE FORMATE
}

Try this,
let datestring = "2019-06-13" // try 13-06-2019 as well
let dateformatter = DateFormatter()
dateformatter.dateFormat = "yyyy-MM-dd"
var date = dateformatter.date(from: datestring)
if date == nil {
dateformatter.dateFormat = "dd-MM-yyyy"
date = dateformatter.date(from: datestring)
}
print(date!)
there should be many other ways to this and hope this will help to you.

You don't need to worry about whatever the format is, You just need to populate it into Date Object by using date formatter. After that you can use that Date Object where ever you want to use. Like
let datestring = "2019-06-13"
let dateformatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
var date1 = formatter.date(from: dateString)
and for other type of date
let dateString = "13-06-2019"
let dateformatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy"
var date2 = formatter.date(from: dateString)
Now you got 2 date objects date1 & date2, use these in your code without bothering about their previous formats.

Related

Converting Date String to different Format

I have myString "28-OCT-22"
I need to convert it to different format "dd.MM.yyyy"
What I've tried:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "d-MMM-yy"
let date = dateFormatter.date(from: myString)! //error, I think because OCT is uppercase, NSDateFormatter doesn't have this format?
dateFormatter.dateFormat = "dd.MM.yyyy"
let stringDate = dateFormatter.string(from: date)
and is there more clean way to do this?
Month abbreviations are localized.
Set the locale to the fixed generic value en_US_POSIX which supports OCT
let myString = "28-OCT-22"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "d-MMM-yy"
let date = dateFormatter.date(from: myString)!
dateFormatter.dateFormat = "dd.MM.yyyy"
let stringDate = dateFormatter.string(from: date)
Although the format is correct force unwrapping converted dates is not recommended

Compare two date - Swift

How can I convert string like (2019-11-02) without time to date format and get current Date device without time then compare with together?
As well as the above using string representations of date, you can actually work with just the dates themselves. Just converting a the string will give you a "start of day" date. The Calendar has a method which will do the same with a date, allowing you to compare the converted string to 'today'
func isDateToday(dateString: String) -> Bool {
let df = DateFormatter()
df.dateFormat = "yyyy-MM-dd"
let date = df.date(from: dateString)
let today = Calendar.current.startOfDay(for: Date())
return date == today
}
You can convert the string to date using a dateFormatter then compare with the current date using an if statement
import Foundation
//convert string to date
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let myDate = dateFormatter.date(from: "2019-11-02")
//convert today's date in the same formate
let currentFormatter = DateFormatter()
currentFormatter.dateStyle = .short
currentFormatter.dateFormat = "yyyy-MM-dd"
let today = currentFormatter.string(from: Date())
let todayDate = dateFormatter.date(from: today)
//Compare the two date's
if myDate == todayDate {
print("ok")
}

Swift - Facebook Graph API: How to convert date as String to hour:min:sec

I get from Facebook-Graph-API, a date like "2019-05-06T08:39:43+0000" as String.
In swift, how to convert this date to, for example if i live in France (+2), "10:39:43" as String too ?
You set the time zone to +2 hours
let str = "2019-05-06T08:39:43+0000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
dateFormatter.timeZone = TimeZone(secondsFromGMT: 7200)
if let date = dateFormatter.date(from: str) {
dateFormatter.dateFormat = "HH:mm:ss"
let strPlus2hours = dateFormatter.string(from: date)
print(strPlus2hours)
}

Parsing a Swift String to Date, then Components

I have a date "2017-12-31" as a String.
What I want to get finally is only the month: "12" as a String.
So I thought that I can change it to Date using a date formatter
let formatter = DateFormatter()
formatter.dateFormat = "MM"
What do I do next?
let dateString = "2017-12-31"
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: Calendar.Identifier.iso8601) formatter.timeZone = TimeZone(identifier: TimeZone.autoupdatingCurrent.identifier)
formatter.dateFormat = "yyyy-MM-dd"
let localDate = formatter.date(from: dateString)
formatter.dateFormat = "MM"
let strMonth = formatter.string(from: localDate!)
print("Month is:",strMonth)
Another way
let dateString = "2017-12-31"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let localDate = formatter.date(from: dateString)
let month = String(NSCalendar.current.component(.month, from: localDate!))
print(month)
First you have to use the DateFormatter to create a temporary Date object from your source String object. Then you have to use it to create your final String from the temporary Date object.
let dateString = "2017-12-31"
let dateFormatter = DateFormatter()
// set the dateFormatter's dateFormat to the dateString's format
dateFormatter.dateFormat = "yyyy-MM-dd"
// create date object
guard let tempDate = dateFormatter.date(from: dateString) else {
fatalError("wrong dateFormat")
}
// set the dateFormatter's dateFormat to the output format you wish to receive
dateFormatter.dateFormat = "LL" // LL is the stand-alone month
let month = dateFormatter.string(from: tempDate)
Use below function for getting month from string file of date
func getMonthFromDateString(strDate: String) -> String {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let date = formatter.date(from: strDate) // Convert String File To Date
formatter.dateFormat = "MM"
let strMM = formatter.string(from: date!) // Convert date to string
return strMM
}

DateFormatter returns nil

Why does DateFormatter return nil?
I think the string format matches?
let dateString = ("01/05/2017")!
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone.current
dateFormatter.dateFormat = "dd/MMM/yyyy"
let dateObj = dateFormatter.date(from: dateString)
After executing the code above, the dateObj is nil.
For month you need to use MM because MMM is used when you having month in the format like Jan,Feb,Mar and so on. So your dateFormat should be dd/MM/yyyy.
let dateString = "01/05/2017"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"
let dateObj = dateFormatter.date(from: dateString)
Note: No need to set timeZone to TimeZone.current
Replace with your dateFormat:
dateFormatter.dateFormat = "dd/MM/yyyy"
date Formatter should be dateFormatter.dateFormat = "dd/MM/yyyy" in this case.
You can check various date format from given link
NSDateFormatter
For given example you can use let formaterStrig = "dd/MM/yyyy"