0

Hie, I have a timestamp such as this one: 2022-01-12T17:06:37.000Z which I obviously want to display in a readable format to the end-user. What I do usually is to check if the date is today, if it is then I take the time from the Timestamp and display it. If it isn't then I display the extract the day and month from the Timestamp and display that. Now the issue is some dates are from last year (2021), so what I want to do is check if the date is from the current year, if it isn't then I want to display the day-month-year. My Current Implementation was built last year before I started dealing with dates from the previous year. Below is my current Implementation:

func DateTimeDisplay(_ stringDate: String) -> some View 
{

    let fromReponseDateformatter = DateFormatter()
    let todayDateformatter = DateFormatter()
    let notTodayDateformatter = DateFormatter()
    let notCurrentYearDateformatter = DateFormatter()
    
    fromReponseDateformatter.calendar = Calendar(identifier: .iso8601)
    fromReponseDateformatter.locale = Locale(identifier: "en_US_POSIX")
    fromReponseDateformatter.timeZone = TimeZone(secondsFromGMT: 0)
    fromReponseDateformatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX"
    
    todayDateformatter.dateFormat = "HH':'mm a"
    
    todayDateformatter.amSymbol = "AM"
    todayDateformatter.pmSymbol = "PM"
    
    notTodayDateformatter.dateFormat = "dd'/'MM"
    
    notCurrentYearDateformatter.dateFormat = "dd'/'MM'/'yyyy" // How do I check this part
    
    let today = fromReponseDateformatter.date(from: stringDate)
    
    var message = Text("The date received: \(fromReponseDateformatter.string(from: today!))")
    
    if Calendar.current.isDateInToday(today!)
    {
        message = Text(todayDateformatter.string(from: today!)).font(Font.custom("Poppins-Regular", size: 15))
            .foregroundColor(Color(red: 171/255, green: 171/255, blue: 171/255))
    }
    else
    {
            
        message = Text(notTodayDateformatter.string(from: today!)).font(Font.custom("Poppins-Regular", size: 15))
            .foregroundColor(Color(red: 171/255, green: 171/255, blue: 171/255))
    }
    
    return message
        
}
Asperi
  • 173,274
  • 14
  • 284
  • 455
  • It looks to me like you haven't tried to solve this yourself so some pointers, work with Date objects and not the formatted date string when doing this, use DateComponents to compare years and extract the DateComponents from a Date using a relevant function from the Calendar class – Joakim Danielson Jan 06 '22 at 13:48
  • 1
    @JoakimDanielson check the duplicate post how to check if the date is in the same year month or week `isDate(equalTo:granularity:)` – Leo Dabus Jan 06 '22 at 13:50
  • @JoakimDanielson I'm not working with string formatted date formats as per se, I'm actually fetching data from a REST API and the date is in the Data Type of a String. I'm simply parsing it to a Date. Thereafter, I'm checking if the date is today or not. Thank you for your response – Ashley Dube Jan 06 '22 at 15:18
  • @LeoDabus I tried that answer you mentioned using this: let check = Date.isInSameYear(today!) and I didn't get my expected boolean response. May you kindly assist? – Ashley Dube Jan 06 '22 at 15:28
  • It looks like I misunderstood one of your comments in the code yes so disregard that part of my comment – Joakim Danielson Jan 06 '22 at 15:29
  • You should do `Date().isInSameYear(today!)` or perhaps better `today?.isInSameYear(.now)` and maybe you want to rename that variable for less confusion – Joakim Danielson Jan 06 '22 at 15:31
  • @JoakimDanielson I appreciate your assistance in resolving my issue. Have a good one. – Ashley Dube Jan 06 '22 at 15:43
  • @AshleyDube `let result = Calendar(identifier: .iso8601).isDate(date1, equalTo: date2, toGranularity: .year)` – Leo Dabus Jan 06 '22 at 16:01

0 Answers0