Swift 3 changing date format
Your code is completely wrong. Just do the following:
@IBAction func datePickerChanged(_ sender: UIDatePicker) {
let formatter = DateFormatter()
formatter.dateFormat = "dd.MM.yyyy HH:mm"
dateLabel.text = formatter.string(from: sender.date)
}
This will convert the date picker's chosen date to a string in the format dd.MM.yyyy HH:mm
in local time.
Never use the description
method to convert any object to a user presented value.
Date Format in Swift
You have to declare 2 different NSDateFormatters
, the first to convert the string to a NSDate
and the second to print the date in your format.
Try this code:
let dateFormatterGet = NSDateFormatter()
dateFormatterGet.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateFormatterPrint = NSDateFormatter()
dateFormatterPrint.dateFormat = "MMM dd,yyyy"
let date: NSDate? = dateFormatterGet.dateFromString("2016-02-29 12:24:26")
print(dateFormatterPrint.stringFromDate(date!))
Swift 3 and higher:
From Swift 3 NSDate
class has been changed to Date
and NSDateFormatter
to DateFormatter
.
let dateFormatterGet = DateFormatter()
dateFormatterGet.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateFormatterPrint = DateFormatter()
dateFormatterPrint.dateFormat = "MMM dd,yyyy"
if let date = dateFormatterGet.date(from: "2016-02-29 12:24:26") {
print(dateFormatterPrint.string(from: date))
} else {
print("There was an error decoding the string")
}
How to change the time format in calendar swift 3
Set the FSCalendar deleget and try this code:
func calendar(_ calendar: FSCalendar, shouldSelect date: Date, at monthPosition: FSCalendarMonthPosition) -> Bool {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
let dateString = dateFormatter.string(from: date as Date)
print("Selected Date: \(dateString)")
return true
}
Unable to change format of date in swift
You are doing it wrong. If you need to show the date, you use a formatted String
and not the Date
object.
func getDate() -> String {
let formatter = DateFormatter()
// Set the appropriate time zone or locale
formatter.dateFormat = "dd/MM/yyyy HH:mm"
return formatter.string(from: self)
}
How to convert date format from dd/MM/YYYY to YYYY-MM-dd in swift
First changes your year
formatter with yyyy
and instead of using two NSDateFormatter
use just one like this
let inputFormatter = NSDateFormatter()
inputFormatter.dateFormat = "MM/dd/yyyy"
let showDate = inputFormatter.dateFromString("07/21/2016")
inputFormatter.dateFormat = "yyyy-MM-dd"
let resultString = inputFormatter.stringFromDate(showDate!)
print(resultString)
For Swift3 and later
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy"
let date = dateFormatter.date(from: date)
dateFormatter.dateFormat = "yyyy-MM-dd"
let resultString = dateFormatter.string(from: date!)
print(resultString)
How to convert date string into date in swift 3
Use this string class extension
extension String {
func date(format: String) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = format
dateFormatter.timeZone =TimeZone.current
let date = dateFormatter.date(from: self)
return date
}
}
use above function like this
let date = "07/06/2017 11:23 AM".date(format:"MM/dd/yyyy HH:mm a")
Date formatting not working in Swift 3
Date().toString() // convert date to string with userdefined format.
extension Date {
func toString() -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM dd yyyy"
return dateFormatter.string(from: self)
}
}
Related Topics
Uiimage Aspect Fill When Using Drawinrect
Objective-C Call Swift Function
Exc_Bad_Access in Parent Class Init() with Xcode 10.2
Skspritenode Subclassing Swift
Transit Mkdirectionsrequest Produces Null Error Error Domain=Mkerrordomain Code=5 "(Null)"
Swift How to "Pass by Value" of a Object
Cannot Convert Value of Type 'String.Type' to Expected Argument Type 'String!'
How to Change the Nstimeinterval of an Nstimer After X Seconds
How to Share Published Model Between Two View Models in Swiftui
How to Pass in a Void Block to Objc_Setassociatedobject in Swift
Change Push Notification Sound File, Only Works After iOS Reboot
Swipe to Delete on a Tableview That Is Inside a Pageviewcontroller
My App Is Rejected with Exception Exc_Breakpoint (Sigtrap)
How to Make a Uiimage Scrollable Inside a Uiscrollview
How to Download a PDF File in Swift and Find in File Manager