1

How can i get time with below format -

yyyy-MM-dd 00:00:00 +0000

currently i am getting

2018-04-16 11:27:06 +Greenwich Mean Time

by below code -

let nowDate = Date()
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "GMT")
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss +zzzz"
print(dateFormatter.string(from: nowDate))

The problem is time offset i am looking for 00:00:00 +0000

Ramesh
  • 147
  • 1
  • 17
  • You mean to say you want time in yyyy-MM-dd 00:00:00 +0000 in this format.? – Abhirajsinh Thakore Apr 16 '18 at 11:33
  • You can use `DateComponents` or `Calendar` to set the Hour/Minutes/Seconds to 0 on `nowDate`: https://stackoverflow.com/questions/26189656/how-can-i-set-an-nsdate-object-to-midnight For the +0000 check there: http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns – Larme Apr 16 '18 at 11:33
  • for additional info see this : http://nsdateformatter.com/ – Anbu.Karthik Apr 16 '18 at 11:33
  • @Anbu.karthik `let now = Date() let caln = Calendar(identifier: .gregorian) let updated = caln.startOfDay(for: now) print(updated)//2018-04-15 18:30:00 +0000` why it's not giving with zero offset :( – Ramesh Apr 16 '18 at 11:40
  • try this: func toLocalTime() -> Date { let timezone: TimeZone = TimeZone.current let seconds: TimeInterval = TimeInterval(timezone.secondsFromGMT()) return Date(timeInterval: seconds, since: self) } – A.Munzer Apr 16 '18 at 11:45
  • @Larme `let date: Date = Date() let cal: Calendar = Calendar(identifier: .gregorian) let newDate: Date = cal.date(bySettingHour: 0, minute: 0, second: 0, of: date)! print(newDate)// it gives 2018-04-15 18:30:00 +0000 instead `00:00:00 +0000` – Ramesh Apr 16 '18 at 12:11
  • 2
    "yyyy-MM-dd HH:mm:ss Z" – Ahmad Labeeb Apr 16 '18 at 12:22

0 Answers0