How to Convert a Persian Date into a Gregorian in Swift

How to convert a Persian date into a Gregorian in swift

Update for Swift 3:

let formatter = DateFormatter()
formatter.dateFormat = "dd/MM/yyyy"
formatter.calendar = Calendar(identifier: .gregorian)
let date = Date()
print("Converted date to Grogrian = \(formatter.string(from: date))")

formatter.calendar = Calendar(identifier: .persian)
formatter.dateFormat = "yyyy/MM/dd"
print("Converted date to Hijri = \(formatter.string(from: date))")

Here are alternatives:

  • era: G (AD), GGGG (Anno Domini)
  • year: y (2018), yy (18), yyyy (2018)
  • month: M, MM, MMM, MMMM, MMMMM
  • day of month: d, dd
  • day name of week: E, EEEE, EEEEE, EEEEEE

how can I change Hijri(Islamic Calendar) string to Gregorian date in swift

To parse the Islamic date string, you need a DateFormatter whose calendar property is set to the Islamic calendar.

To produce the Gregorian date string, you'll need a DateFormatter whose calendar is set to the Gregorian calendar. You can use the same DateFormatter and change the calendar, or you can use two separate DateFormatters.

Example:

import Foundation

let islamicDateString = "1439/02/1 22:30"

let islamicFormatter = DateFormatter()
islamicFormatter.dateFormat = "yyyy/MM/dd HH:mm"
// Note that there are actually four different Islamic calendars available on Apple platforms:
// .islamic, .islamicCivil, .islamicTabular, and .islamicUmmAlQura
// I don't know what the differences are.
islamicFormatter.calendar = Calendar(identifier: .islamic)

let gregorianFormatter = DateFormatter()
gregorianFormatter.calendar = Calendar(identifier: .gregorian)
gregorianFormatter.dateStyle = .short
gregorianFormatter.timeStyle = .short

if let date = islamicFormatter.date(from: islamicDateString) {
let gregorianString = gregorianFormatter.string(from: date)
print(gregorianString)
} else {
print("I couldn't parse \(islamicDateString)")
}

Output:

10/20/17, 10:30 PM

Note that the output here depends on my time zone. You can also set each formatter's timeZone property if you don't want to use the user's time zone.

Convert gregorian datepicker to persian datepicker in swift?

You need to set the calendar, not the dateFormat:

dateFormatter.calendar = NSCalendar(identifier: NSCalendarIdentifierPersian)

You would also have to set the locale property if you want the language to change:

dateFormatter.locale = NSLocale(localeIdentifier: "fa_IR")

Also, as the comment on the question notes, you're working with an NSDateFormatter, not a UIDatePicker in your code. Fortunately the answer is still correct; UIDatePicker also has a calendar property that you would set to accomplish this goal.

How to convert Gregorian dates to Chinese (not Sexagenary Cycle) in Swift

Your code is also fine. Just change format to this:

formatter.dateFormat = "MM/dd/YYYY"

This is how I convert dates.

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/YYYY"
dateFormatter.calendar = Calendar(identifier: .gregorian)
let date = Date()
let dateInGregorian = dateFormatter.string(from: date)

print(dateInGregorian)

dateFormatter.calendar = Calendar(identifier: .chinese)
dateFormatter.dateFormat = "dd/MM/YYYY"
print("Converted date to Chinese = \(dateFormatter.string(from: date))")

To convert to Gregorian:

dateFormatter.calendar = Calendar(identifier: .gregorian)
dateFormatter.dateFormat = "dd/MM/YYYY"
print("Converted date to Gregorian = \(dateFormatter.string(from: date))")

We have different format when working with chinese date, use "dd MMM YYYY". To convert Chinese to Gregorian

func convertDateFrom(dateString: String, dateFormat: String) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = dateFormat
dateFormatter.locale = Locale(identifier: "en_us")
guard let date = dateFormatter.date(from: dateString) else {return nil}
return date
}

let dateGregorian = convertDateFrom(dateString: "09/03/4657", dateFormat: "dd MMM YYYY") ?? Date()

print("Converted date to Gregorian = \(dateGregorian)")

How convert Gregorian date to Persian date?

Use the PersianCalendar:

string GregorianDate = "Thursday, October 24, 2013";
DateTime d = DateTime.Parse(GregorianDate);
PersianCalendar pc = new PersianCalendar();
Console.WriteLine(string.Format("{0}/{1}/{2}", pc.GetYear(d), pc.GetMonth(d), pc.GetDayOfMonth(d)));

Convert string to date in Swift

  • Convert the ISO8601 string to date

      let isoDate = "2016-04-14T10:44:00+0000"

    let dateFormatter = DateFormatter()
    dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
    let date = dateFormatter.date(from:isoDate)!
  • Get the date components for year, month, day and hour from the date

      let calendar = Calendar.current
    let components = calendar.dateComponents([.year, .month, .day, .hour], from: date)
  • Finally create a new Date object and strip minutes and seconds

      let finalDate = calendar.date(from:components)

Consider also the convenience formatter ISO8601DateFormatter introduced in iOS 10 / macOS 10.12:

let isoDate = "2016-04-14T10:44:00+0000"

let dateFormatter = ISO8601DateFormatter()
let date = dateFormatter.date(from:isoDate)!

islamicUmmAlQura UIDatePicker is not working properly

I have an extension on github to convert dates. Have a look. mine is persian date so you just need to replace the below code with mine inside the extension:

datePicker.calendar = Calendar(identifier: .islamicUmmAlQura)

[https://github.com/Fridday/PersianDatePicker][1]



Related Topics



Leave a reply



Submit