TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Dates Show 0.000000

Forums > Swift

This is my code I am parsing a CSV Filewhen i display the ImportLogItem on the View as a list it show 0.000000 when i try to pass it as a date any advice, and if i pass it as a string is show the date in a "12 Jun 2024" format. it shows a "6/12/2024" in the cell, but when i display the ImportLogItem on the View as a list it show 0.000000 any advice?

  for row in rows {
        if isFirstLine {
            isFirstLine = false
            continue
        }

        let columns = row.split(separator: ",")
        guard columns.count == 7 else {
            // Skip rows with an invalid number of columns
            continue
        }
        //creates string
        let dofString = columns[1]
        //Create Date Formatter
        let dateFormatter = DateFormatter()
        //set date format
        dateFormatter.dateFormat = "MM/DD/YYYY"

        let dofDate = dateFormatter.date(from: String(dofString))

        let item = ImportLogItem(
            id: identifier,
            dof: dofDate?.timeIntervalSince1970 ?? 0,
            acft: String(columns[0]),
            duty: String(columns[2]),
            condition: String(columns[3]),
            hours: TimeInterval(columns[5]) ?? 0,
            createdDate: Date().timeIntervalSince1970
        )
        items.append(item)
    }

    return items
}

   

We really need more info before we can answer.

How does the date look in the CSV?

What is the code you are using to display the date?

Why are you storing your dof and createdDate properties as Doubles by using timeIntervalSince1970 instead of storing them as Dates?

Are you converting these Doubles back into Dates before you display them or are you displying them as Doubles?

Too many questions.

   

@roosterboy your questions helped me i was looking at what was in the cell not what the cell was displaying in the CSV file

i changed the

dateFormatter.dateFormat = "MM/DD/YYYY" to
            dateFormatter.dateFormat = "dd-MMM-yyyy"

and it is showing correctly now!

This is how i am dislplaying the date
Text(formattedDate(from: viewModel.importLogItems[index].dof))
This is the formattedDate function i am using to display it

      let dateFormatter = DateFormatter()
      dateFormatter.dateFormat = "dd MMM yyyy"
        return dateFormatter.string(from: Date(timeIntervalSince1970: timestamp))
}

I am saving the dates as doubles to firestore and to sum values based on a range like this:

        guard let startDate = oneYearPriorToBirthday, let endDate = sixMonthsPriorToBirthday else {
            return 0.0
        }
        return totalHours(inDateRange: startDate, endDate: endDate)
    }

If anyone anyother suggestions I would love to hear them sorry for not including enough information in the original post.

   

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.