Automated convert string into date [closed]
How can I automated convert string to date with this code
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
guard let date = dateFormatter.date(from: String) else { return }
so I don't have to type again or reuse again, I was thinking with DateFormatter extension but I don't know how or is that the correct method. Thank you for your attention
ios swift date nsdateformatter
closed as unclear what you're asking by rmaddy, Code Different, El Tomato, dahiya_boy, Tamás Sengel Nov 26 '18 at 6:13
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 1 more comment
How can I automated convert string to date with this code
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
guard let date = dateFormatter.date(from: String) else { return }
so I don't have to type again or reuse again, I was thinking with DateFormatter extension but I don't know how or is that the correct method. Thank you for your attention
ios swift date nsdateformatter
closed as unclear what you're asking by rmaddy, Code Different, El Tomato, dahiya_boy, Tamás Sengel Nov 26 '18 at 6:13
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Do you need only hours and min from date object?
– iOS_Maccus
Nov 26 '18 at 4:29
What do you mean by automated convert ?
– Shubham Bakshi
Nov 26 '18 at 4:33
@HPM I need to convert to date because I got a string time like "12:00"
– ferryawijayanto
Nov 26 '18 at 4:33
@ShubhamBakshi I have a 5 different time of string like this for example"12:00", "14:00" etc. so if I format it and convert it into date by typing over and over is not effective
– ferryawijayanto
Nov 26 '18 at 4:35
Create a function that take a string date as input and provide the output as a Date date ? OR you can create an extension ofString
class and create function with name likeconvertToDate()
and use that !
– Shubham Bakshi
Nov 26 '18 at 4:38
|
show 1 more comment
How can I automated convert string to date with this code
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
guard let date = dateFormatter.date(from: String) else { return }
so I don't have to type again or reuse again, I was thinking with DateFormatter extension but I don't know how or is that the correct method. Thank you for your attention
ios swift date nsdateformatter
How can I automated convert string to date with this code
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
guard let date = dateFormatter.date(from: String) else { return }
so I don't have to type again or reuse again, I was thinking with DateFormatter extension but I don't know how or is that the correct method. Thank you for your attention
ios swift date nsdateformatter
ios swift date nsdateformatter
edited Nov 26 '18 at 3:57
ferryawijayanto
asked Nov 26 '18 at 3:49
ferryawijayantoferryawijayanto
1008
1008
closed as unclear what you're asking by rmaddy, Code Different, El Tomato, dahiya_boy, Tamás Sengel Nov 26 '18 at 6:13
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by rmaddy, Code Different, El Tomato, dahiya_boy, Tamás Sengel Nov 26 '18 at 6:13
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Do you need only hours and min from date object?
– iOS_Maccus
Nov 26 '18 at 4:29
What do you mean by automated convert ?
– Shubham Bakshi
Nov 26 '18 at 4:33
@HPM I need to convert to date because I got a string time like "12:00"
– ferryawijayanto
Nov 26 '18 at 4:33
@ShubhamBakshi I have a 5 different time of string like this for example"12:00", "14:00" etc. so if I format it and convert it into date by typing over and over is not effective
– ferryawijayanto
Nov 26 '18 at 4:35
Create a function that take a string date as input and provide the output as a Date date ? OR you can create an extension ofString
class and create function with name likeconvertToDate()
and use that !
– Shubham Bakshi
Nov 26 '18 at 4:38
|
show 1 more comment
Do you need only hours and min from date object?
– iOS_Maccus
Nov 26 '18 at 4:29
What do you mean by automated convert ?
– Shubham Bakshi
Nov 26 '18 at 4:33
@HPM I need to convert to date because I got a string time like "12:00"
– ferryawijayanto
Nov 26 '18 at 4:33
@ShubhamBakshi I have a 5 different time of string like this for example"12:00", "14:00" etc. so if I format it and convert it into date by typing over and over is not effective
– ferryawijayanto
Nov 26 '18 at 4:35
Create a function that take a string date as input and provide the output as a Date date ? OR you can create an extension ofString
class and create function with name likeconvertToDate()
and use that !
– Shubham Bakshi
Nov 26 '18 at 4:38
Do you need only hours and min from date object?
– iOS_Maccus
Nov 26 '18 at 4:29
Do you need only hours and min from date object?
– iOS_Maccus
Nov 26 '18 at 4:29
What do you mean by automated convert ?
– Shubham Bakshi
Nov 26 '18 at 4:33
What do you mean by automated convert ?
– Shubham Bakshi
Nov 26 '18 at 4:33
@HPM I need to convert to date because I got a string time like "12:00"
– ferryawijayanto
Nov 26 '18 at 4:33
@HPM I need to convert to date because I got a string time like "12:00"
– ferryawijayanto
Nov 26 '18 at 4:33
@ShubhamBakshi I have a 5 different time of string like this for example"12:00", "14:00" etc. so if I format it and convert it into date by typing over and over is not effective
– ferryawijayanto
Nov 26 '18 at 4:35
@ShubhamBakshi I have a 5 different time of string like this for example"12:00", "14:00" etc. so if I format it and convert it into date by typing over and over is not effective
– ferryawijayanto
Nov 26 '18 at 4:35
Create a function that take a string date as input and provide the output as a Date date ? OR you can create an extension of
String
class and create function with name like convertToDate()
and use that !– Shubham Bakshi
Nov 26 '18 at 4:38
Create a function that take a string date as input and provide the output as a Date date ? OR you can create an extension of
String
class and create function with name like convertToDate()
and use that !– Shubham Bakshi
Nov 26 '18 at 4:38
|
show 1 more comment
1 Answer
1
active
oldest
votes
You need to create extension on String
instead of Dateformatter
.
Try with below code,
extension String {
var date: Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
return dateFormatter.date(from: self)
}
}
Uses example:
override func viewDidLoad() {
let dateString = "11:32"
if let date = dateString.date { //This will return Date object
debugPrint(date)
}
let invalidDateString = "XYZ:ABC"
if let invalidDate = invalidDateString.date { //This will return nil as date string is invalid
debugPrint(invalidDate)
}
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to create extension on String
instead of Dateformatter
.
Try with below code,
extension String {
var date: Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
return dateFormatter.date(from: self)
}
}
Uses example:
override func viewDidLoad() {
let dateString = "11:32"
if let date = dateString.date { //This will return Date object
debugPrint(date)
}
let invalidDateString = "XYZ:ABC"
if let invalidDate = invalidDateString.date { //This will return nil as date string is invalid
debugPrint(invalidDate)
}
}
add a comment |
You need to create extension on String
instead of Dateformatter
.
Try with below code,
extension String {
var date: Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
return dateFormatter.date(from: self)
}
}
Uses example:
override func viewDidLoad() {
let dateString = "11:32"
if let date = dateString.date { //This will return Date object
debugPrint(date)
}
let invalidDateString = "XYZ:ABC"
if let invalidDate = invalidDateString.date { //This will return nil as date string is invalid
debugPrint(invalidDate)
}
}
add a comment |
You need to create extension on String
instead of Dateformatter
.
Try with below code,
extension String {
var date: Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
return dateFormatter.date(from: self)
}
}
Uses example:
override func viewDidLoad() {
let dateString = "11:32"
if let date = dateString.date { //This will return Date object
debugPrint(date)
}
let invalidDateString = "XYZ:ABC"
if let invalidDate = invalidDateString.date { //This will return nil as date string is invalid
debugPrint(invalidDate)
}
}
You need to create extension on String
instead of Dateformatter
.
Try with below code,
extension String {
var date: Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
return dateFormatter.date(from: self)
}
}
Uses example:
override func viewDidLoad() {
let dateString = "11:32"
if let date = dateString.date { //This will return Date object
debugPrint(date)
}
let invalidDateString = "XYZ:ABC"
if let invalidDate = invalidDateString.date { //This will return nil as date string is invalid
debugPrint(invalidDate)
}
}
answered Nov 26 '18 at 4:38
iOS_MaccusiOS_Maccus
30710
30710
add a comment |
add a comment |
Do you need only hours and min from date object?
– iOS_Maccus
Nov 26 '18 at 4:29
What do you mean by automated convert ?
– Shubham Bakshi
Nov 26 '18 at 4:33
@HPM I need to convert to date because I got a string time like "12:00"
– ferryawijayanto
Nov 26 '18 at 4:33
@ShubhamBakshi I have a 5 different time of string like this for example"12:00", "14:00" etc. so if I format it and convert it into date by typing over and over is not effective
– ferryawijayanto
Nov 26 '18 at 4:35
Create a function that take a string date as input and provide the output as a Date date ? OR you can create an extension of
String
class and create function with name likeconvertToDate()
and use that !– Shubham Bakshi
Nov 26 '18 at 4:38