I have assigned the value of date to a dictionary like so...
userDetailsDictionary["birth_date"] = "\(birthDateVar)"
Now this gives the date in dd-mm-yyyy. But I want the date in yyyy-mm-dd. For that I tried something like this...
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
let bday = dateFormatter.date(from: birthDateVar)
dateFormatter.dateFormat = "yyyy-MM-dd"
let bdayString = dateFormatter.string(from: bday!)
Though birthDateVar has the value in it, while I'm trying to convert it, it is showing as nil and so bday is becoming nil. Confused as to what the reason is...
change your date format from
dateFormatter.dateFormat = "MM-dd-yyyy"
to
dateFormatter.dateFormat = "dd-MM-yyyy"
Updated answer
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") as Locale!
let bday = dateFormatter.date(from: birthDateVar)
dateFormatter.dateFormat = "yyyy-MM-dd"
let bdayString = dateFormatter.string(from: bday!)
Related
The date I'm having is this..
2021-03-05T12:21:18
But I want it in dd-MM-yyyy format. This is what I did for that...
dateFormatter.dateFormat = "YYYY-MM-dd'T'HH:mm:ss"
let date2 = dateFormatter.date(from: self.Due_Date) //self.Due_Date is 2021-03-05T12:21:18
dateFormatter.dateFormat = "dd-MM-yyyy"
self.Due_Date = dateFormatter.string(from: date2!) //CRASH HERE
But I get date2 as nil. So the code crashes at self.Due_Date = dateFormatter.string(from: date2!)
What could be the reason for this...?
The “yyyy” represents the calendar year of the date while the “YYYY” represents the year of the week.
for detail information, please see this Difference between 'YYYY' and 'yyyy' in NSDateFormatter
let getInputString = "2021-03-05T12:21:18"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
if let getInputDate = dateFormatter.date(from:getInputString){
dateFormatter.dateFormat = "dd-MM-yyyy"
let getoutputString = dateFormatter.string(from: getInputDate)
}
Note : as per #LeoDabus guidelines please ensure the self.Due_Date is empty or not before start the conversion.
I cannot seem to figure out why only "24:00" returns nil instead of 12:00AM, whereas "23:59" returns correctly as 11:59PM.
let dateAsString = "24:00"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") as Locale
let date = dateFormatter.date(from: dateAsString)
dateFormatter.dateFormat = "h:mm a"
let Date12 = dateFormatter.string(from: date!)
print(Date12)
You are using the wrong date format for the hour component. For values from 1-24 you have to use k, if you have a leading zero you need to use kk:
let dateAsString = "24:00"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "kk:mm"
if let date = dateFormatter.date(from: dateAsString) {
dateFormatter.dateFormat = "h:mm a"
let date2 = dateFormatter.string(from: date)
print(date2) // "12:00 AM\n"
}
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
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)")
I try to format the time in French in my iOS app written in Swift 3
Here the data string
2000-01-01T14:00:00.000Z
Here method swift 3
func get_time_start() -> String {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "fr_FR")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
let timeStart = dateFormatter.date(from: time_start)
dateFormatter.dateFormat = "hh:mm"
return dateFormatter.string(from: timeStart!);
}
On my application gives its 2:00
And I want to display 2:00 p.m.
I used it: dateFormatter.locale = Locale (identifier: "fr_FR")
but its not working there have another manipulation do?
You need to fix both date formats:
func get_time_start() -> String {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "fr_FR")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let timeStart = dateFormatter.date(from: time_start)
dateFormatter.dateFormat = "hh:mm a"
return dateFormatter.string(from: timeStart!);
}
Better yet, don't use dateFormat to convert the Date to a String. Instead, set the timeStyle so it's formatted properly without the need to force a specific format.
func get_time_start() -> String {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "fr_FR")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let timeStart = dateFormatter.date(from: time_start)
dateFormatter.dateFormat = nil
dateFormatter.timeStyle = .medium
dateFormatter.dateStyle = .none
return dateFormatter.string(from: timeStart!);
}
In either case, only set the locale if you want all users to see the time formatted in that specific locale (and that would be rare).