Swift Format Date - swift

This is probably really simple, I have tried lots of different methods but it doesnt seem to work.
I have the following date format in a text field:
Saturday, 2 January 2016 12:00
held as:
var str = "\(dobTextField.text!)"
I would like to convert the string above into the format:
YYYY-mm-dd
any help would be much appreciated!

For Swift 2
Check this :
let str = "Saturday, 2 January 2016 12:00"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEEE, d MMMM yyyy HH:mm"
dateFormatter.timeZone = NSTimeZone(name: "UTC")
let outputA = dateFormatter.dateFromString(str)
dateFormatter.dateFormat = "yyyy-MM-dd"
println(dateFormatter.stringFromDate(outputA!))
Output :
2016-01-02

Related

How can i Convert (2020-02-21 06:07:21 +0000) to "21 Feb 2021 12:00 AM"?

I am using device time to get date components. this works fine for 12 hours formate but if user changes device time to 24 hours it shows wrong calculations. used the following method to convert but it always return nil.
let dateAsString = "\(Date())"
let df = DateFormatter()
df.dateFormat = "yyyy-mm-dd HH:mm:ss zzz"
let date = df.date(from: dateAsString)
df.dateFormat = "d MMM yyyy h:mm a"
let time12 = df.string(from: date!)
print(time12)
So, the core problem you're having is the fact that yyyy-mm-dd is using mm which is minutes and not using MM which is months.
So, if instead, you tried something like...
let dateAsString = "\(Date())"
let df = DateFormatter()
df.dateFormat = "yyyy-MM-dd HH:mm:ss zzz"
let date = df.date(from: dateAsString)
df.dateFormat = "d MMM yyyy h:mm a"
let time12 = df.string(from: date!)
it would result in
21 Feb 2020 6:00 PM
Now, the problem is, you could simply do
let df = DateFormatter()
df.dateFormat = "d MMM yyyy h:mm a"
df.string(from: Date())
and get the same result.
Remember, Date does not, in of itself, have a concept of "format", beyond what its "debug" output provides, so you can't change a Date's format, instead, you use a formatter to "represent" the value of the Date in some human readable form.
I have just tried below it works fine on both cases. with AM or PM or 24Format and if you remove a in formatter.dateFormat = "dd MMM yyyy HH:mm a" gives you 24 Format. Even I am using this in my code.
let someDate = Date()
let formatter = DateFormatter()
formatter.dateFormat = "dd MMM yyyy HH:mm a"
let someDateTime = formatter.string(from: someDate)
print(someDateTime)
Print:
21 Feb 2020 09:35 AM
Try this:
let date : "2021-01-20T21:40:59.416Z"
func setDate(date: String) -> String {
var interval: Double = 0
let convertedDate = date.components(separatedBy: ":")
for (index, part) in convertedDate.reversed().enumerated() {
interval += (Double(part) ?? 0) * pow(Double(60), Double(index))
}
let date = Date(timeIntervalSinceNow: interval)
let formatter = Foundation.DateFormatter()
formatter.dateFormat = "LLLL dd, HH:mm at"
return formatter.string(from: date)}
This will return a date in this format
--> "January 21, 05:30 am"
Try,
let str = "2020-02-21 06:07:21 +0000"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-mm-dd hh:mm:ss zzz"
if let date = formatter.date(from: str) {
formatter.dateFormat = "dd MMM yyyy hh:mm a"
let formattedDate = formatter.string(from: date)
print(formattedDate)
}
Swift-this answer helped me
swift - how to convert date from am/pm to 24 hour format
// posting my work here aswell.
i had device formate in 24H.
i needed in 12H.
so i just converted date object to required formate with this code snippet.
let df = DateFormatter()
df.dateFormat = "d MMM yyyy h:mm a"
let time12 = df.string(from: Date())
print(time12)

How do you display date in this format? "Jul 18, 2018 at 10:02 AM"

let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "UTC")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date = dateFormatter.date(from: "2018-07-18T17:02:02.614Z")
date description prints: 2015-06-18 17:02:02 +0000
Playground seems to naturally outputs this on the right side: "Jun 18, 2015 at 10:02 AM"
How do I format it to display this? "Jul 18, 2018 at 10:02 AM"
Thanks!
You need to format date (which is now a Date instance) using another DateFormatter. And you should use date and time styles, not a fixed format for this.
And "UTC" is not a locale, it's a timezone. But you don't need that. But you should use the special locale of en_US_POSIX when parsing a fixed format date string.
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date = dateFormatter.date(from: "2018-07-18T17:02:02.614Z")
if let date = date {
let outputFormatter = DateFormatter()
outputFormatter.dateStyle = .medium
outputFormatter.timeStyle = .short
let output = outputFormatter.string(from: date)
print(output)
}
Output:
Jul 18, 2018 at 11:02 AM
Note that the time will depend on your local timezone. By default the output will be in local time so don't expect the output to be 17:02 since that is the time in the UTC timezone.

Time conversion in swift in different format

I want to convert the time string from 15:00:00 AM to 03:00 PM, this is what I am trying to achieve the results.
var str = "15:00:00 AM" // "15:00:00 AM"
let df = DateFormatter()
df.timeZone = TimeZone.current
df.dateFormat = "HH:mm:ss a" // "Jan 1, 2000, 12:00 AM"
let date = df.date(from: str)
let df1 = DateFormatter()
df1.dateFormat = "hh:mm a"
str = df1.string(from: date!) // "12:00 AM"
Not sure where I am missing tried different approaches but still I am not able to convert the times correctly.
PS: More of the answer, I am checking for the possibility of the conversion, as the time string I am getting is from server end and I can update them for correction.
Good Morning , please try this code
let now = NSDate()
let df = DateFormatter()
df.dateFormat = "hh:mm a"
let result = df.string(from: now as Date)

Converting String to NSDate in Swift

I have an API sending date in string with universal time but when I try to convert to NSDate format it tends to return nil. I have looked in lot of places but couldn't find a way to resolve this. Below is my code that I have written. Please help me to find where am I going wrong. The string I'm trying to convert is "Sun Feb 14 23:35:40 UTC 2016". The problem causing it to return nil is with the time zone I believe.
let dateFormatter = NSDateFormatter()
dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
dateFormatter.dateFormat = "EEE MMM dd hh:mm:ss yyyy"
dateFormatter.dateFromString("Sun Feb 14 23:35:40 UTC 2016")
Like this:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEE MMM dd kk:mm:ss zzz yyyy"
let d = dateFormatter.dateFromString("Sun Feb 14 23:35:40 UTC 2016")

date playground loses a day: SWIFT

I'm trying to play with dates, saving them as strings, then returning them to dates. my output loses a day. Please see below for the playground code:
let date = NSDate()
let formatter = NSDateFormatter()
let secondDateFormatter = NSDateFormatter()
formatter.dateStyle = .FullStyle
let dateString = formatter.stringFromDate(date)
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEEE, MMMM dd, yyyy"
let finalDate : NSDate = dateFormatter.dateFromString(dateString)!
println(finalDate)
here's the current output:
date = May 7, 2015, 9:16 AM
dateString = "Thursday, May 7, 2015" (perfect)
let finalDate shows: "May 7, 2015, 12:00 AM" (almost perfect except the time)
println(finalDate) reveals : 2015-05-06 16:00:00 +0000
I've searched around and read that it is a time zone modification? I'm not sure. I'll play with it more and see if it works for my needs. Any idea why the output would be different when println is execute vs. just the calculation?