1

like eg. in which format we need to Store and fetch dates? should it will be stored on Server Time? or All Dates Should store in Specific one Time zone ? Didn't know how deal with it.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
NIRAV BHAVSAR
  • 152
  • 12
  • 2
    You would normally store dates in UTC and then you can adjust to the local timezone on the device – Paulw11 Jul 20 '18 at 05:25

1 Answers1

0

Here is the code to convert UTC to Device Local timezone.If your server timezone is Indian then use "IST" abbreviation instead of "UTC".

extension String{
func convertUTCToLocalDate(inputFormat: String, outputFormat: String)-> String{
    let formatter = DateFormatter()
        formatter.dateFormat = inputFormat
            formatter.timeZone = TimeZone(abbreviation: "UTC")
    let oldDate = formatter.date(from: self)
    formatter.dateFormat = outputFormat
    formatter.timeZone = TimeZone.current 
    return (oldDate != nil) ? formatter.string(from: oldDate!) : self
}}

Usage:-

let localDate = "20 Jul, 2018".convertUTCToLocalDate(inputFormat: "dd MMM, yyyy", outputFormat: "dd MMM, yyyy")
OMEESH SHARMA
  • 141
  • 1
  • 8