-2

I have url of having dob's list with names and the response comes in this scenario "1993-03-28"....... along names and I have to show the date as 28 Mar I have created array of months and working but It printing all same dates for all members my code is

 var  dob = "\(arrdata[indexPath.section].dob)"

    print(dob)
    var dobSplit = dob.split(separator:"-")
    print (dobSplit)
    var mont = dobSplit[1]
    var dat = dobSplit[2]
    print(dat,mont)


    var monthArray = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
    print(monthArray)
    print(monthArray[Int(mont)!-1])

    var mo = monthArray[Int(mont)!-1]

    var finalDOB = dat + " " + mo
    print(finalDOB)


 // cell.dateLbl.text = "\(arrdata[indexPath.row+1].dob)"
    // instead of that I gave like this 
     cell.dateLbl.text = finalDOB 

for all names its displaying same dates

EX: Rohit 28 Mar
Rahul 28 Mar
Sohit 28 Mar
Kuldeep 28 Mar

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52

2 Answers2

0

Use DateFormatter to get the required date format like,

func getFormattedDate(from str: String) -> String? {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    if let date = dateFormatter.date(from: str) {
        dateFormatter.dateFormat = "dd MMM"
        return dateFormatter.string(from: date)
    }
    return nil
}

To add the formattedDate in your cell, use indexPath.row instead of indexPath.section, i.e.

let dob = "\(arrdata[indexPath.row].dob)"
cell.dateLbl.text = getFormattedDate(from: dob)

Still if it is printing the same date in every cell of tableView, try adding the array of string dates that you're using for each cell. Also the UITableViewDataSource methods.

PGDev
  • 23,751
  • 6
  • 34
  • 88
  • You should add a locale (`en_US_POSIX`) at least to get the proper month abbreviations and it's recommended for fixed date formats anyway. – vadian Jul 16 '19 at 06:50
  • @vadian This is a case where you don't want that special locale. That's only needed when parsing a string to a `Date` and when that string has English month names and/or it has time. This has neither. – rmaddy Jul 16 '19 at 06:52
  • @rmaddy The explicit `monthArray` indicates that the OP wants English month names and the string **is** parsed to `Date` first. Without the locale you get the month names in the current locale. – vadian Jul 16 '19 at 06:57
  • @vadian 1. The explicit month names could mean the OP isn't aware that that isn't a good idea. It's not proof that they only want English month names for all users. But, then again, maybe they do. 2. In reality, you only get month names in the user's preferred language if the app is localized to that language. – rmaddy Jul 16 '19 at 06:59
  • @PGDev Thanks its working the reason why I kept section means I have another view in that I have to show only first response of table view . – P.VenkataVamsi Jul 16 '19 at 07:01
0

You have a typo. Change:

var  dob = "\(arrdata[indexPath.section].dob)"

to:

var  dob = "\(arrdata[indexPath.row].dob)"

Besides that, use a DateFormatter to convert your date string from one format to another. At a minimum, don't hardcode your own month strings. It doesn't localize well. Use Calendar shortMonthNames if you don't want to do it properly with DateFormatter.

rmaddy
  • 314,917
  • 42
  • 532
  • 579