0

I am trying to construct a date from a string. The date is in .islamic calendar. I am using the following code

let StringValue = "1439/01/02"
//constructing the Hijri Date
let HijriFormatter = DateFormatter ()
HijriFormatter.calendar = Calendar(identifier: .islamic)
HijriFormatter.dateFormat="yyyy/MM/dd"
HijriFormatter.timeZone = TimeZone(abbreviation: "GMT+0:00")
let hijriDate =  HijriFormatter.date(from: StringValue)
print (hijriDate!)

The output should be similar to the string. However, for some reason I am getting the following output:

2017-09-22 00:00:00 +0000

Any idea of why the code is behaving this way

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Jan
  • 747
  • 1
  • 10
  • 29
  • 2
    You print a `Date` – which is an absolute point in time and always printed using UTC. Possible duplicate of https://stackoverflow.com/questions/39937019/nsdate-or-date-shows-the-wrong-time – Martin R Feb 28 '18 at 14:04
  • Martin is right. You can verify that the formatter is setup correctly when you do something like: `HijriFormatter.string(from: hijriDate!)` which will give you a string resembling the original one (since it matches the format passed). – Alladinian Feb 28 '18 at 14:09

1 Answers1

0

You can get date components from hijri date using calendar:

    let calenadr = Calendar(identifier: .islamicUmmAlQura)
    let unitFlags: Set<Calendar.Component> = [.day, .month, .year]
    let comps = calenadr.dateComponents(unitFlags, from: hijriDate!)
    print(comps.day , comps.month, comps.year)
Moayad Al kouz
  • 1,342
  • 1
  • 9
  • 19