Putting a Date in a struct

I have tried a lot of things with no luck on getting this working. The two date vars won't take my date. I think I need to do something with dateformatter but haven't found the right way yet. Can anyone shed some light on this?



struct CCdata {
    var id: Int
    var org_nm: String
    var org_cd: String
    var tms: String
    var tec: String
    var serno: String
    var mcn: String
    var jcn: String
    var rcvd_dt: Date
    var cmp_dt: Date
}


var firstTry = CCdata(id: 45, org_nm: "HSM73", org_cd: "Q30", tms: "MH-60R",
                      tec: "AHZS", serno: "167043", mcn: "1V4YSQU", jcn: "Q30113239",
 rcvd_dt: 4/23/2018 0:00:00, cmp_dt: 5/2/2018 0:00:00)
Answered by Claude31 in 371102022

Not sure to understand your question.


Yes, you can have a date in struc.

But the parameter when you create the instance of strcut must me a Date.


Such as, if you want to use present date:

var firstTry = CCdata(id: 45, org_nm: "HSM73", org_cd: "Q30", tms: "MH-60R",
                      tec: "AHZS", serno: "167043", mcn: "1V4YSQU", jcn: "Q30113239",
                         rcvd_dt: Date(),
                         cmp_dt: Date())


Now, if you want to specifiy your own date, you should create a Date as you specify

let formatter = DateFormatter() 
formatter.dateFormat = "MM/dd/yyyy" 
let cmp_dt = formatter.date(from: "5/2/2018 0:00:00") ?? Date()    // This is a Date ; note that you must unwrap because formatter may fail

var firstTry = CCdata(id: 45, org_nm: "HSM73", org_cd: "Q30", tms: "MH-60R",
                      tec: "AHZS", serno: "167043", mcn: "1V4YSQU", jcn: "Q30113239",
                         rcvd_dt: Date(),
                         cmp_dt: cmp_dt)

Effectively, the parameter you pass is not a Date.


You have several solutions:


// Specify date components
var dateComponents = DateComponents()
dateComponents.year = 2018
dateComponents.month = 5
dateComponents.day = 2
dateComponents.timeZone = TimeZone(abbreviation: "PST") // Set your time zone
dateComponents.hour = 0
dateComponents.minute = 0

// Create date from components
let userCalendar = Calendar.current // user calendar
cmp_dt = userCalendar.date(from: dateComponents)

Do the same for the other date


Or use a formatter

let formatter = DateFormatter()
formatter.dateFormat = "MM/dd/yyyy HH:mm:ss"
cmp_dt = formatter.date(from: "5/2/2018 0:00:00")



Just take care of the timezone.


Get more details here

https://stackoverflow.com/questions/24089999/how-do-you-create-a-swift-date-object

The question modified a little bit with this answer. Can I not have a date type in my struct? Or is it I need to modify the string to a date type with the DateFormatter first?


Can I modify the second line like below to only keep the date part?


Thank you for your help.


let formatter = DateFormatter()  
formatter.dateFormat = "MM/dd/yyyy"  
cmp_dt = formatter.date(from: "5/2/2018 0:00:00") 
Accepted Answer

Not sure to understand your question.


Yes, you can have a date in struc.

But the parameter when you create the instance of strcut must me a Date.


Such as, if you want to use present date:

var firstTry = CCdata(id: 45, org_nm: "HSM73", org_cd: "Q30", tms: "MH-60R",
                      tec: "AHZS", serno: "167043", mcn: "1V4YSQU", jcn: "Q30113239",
                         rcvd_dt: Date(),
                         cmp_dt: Date())


Now, if you want to specifiy your own date, you should create a Date as you specify

let formatter = DateFormatter() 
formatter.dateFormat = "MM/dd/yyyy" 
let cmp_dt = formatter.date(from: "5/2/2018 0:00:00") ?? Date()    // This is a Date ; note that you must unwrap because formatter may fail

var firstTry = CCdata(id: 45, org_nm: "HSM73", org_cd: "Q30", tms: "MH-60R",
                      tec: "AHZS", serno: "167043", mcn: "1V4YSQU", jcn: "Q30113239",
                         rcvd_dt: Date(),
                         cmp_dt: cmp_dt)
Putting a Date in a struct
 
 
Q