jalaliStringToGregorianString function

String? jalaliStringToGregorianString(
  1. String? jalaliDateString,
  2. String seprator
)

Implementation

String? jalaliStringToGregorianString(
    String? jalaliDateString, String seprator) {
  if (jalaliDateString == null || jalaliDateString.isEmpty) {
    return null; // Return null if the input is null or empty
  }

  try {
    // Assuming the input format is "yyyy/mm/dd"
    final List<String> parts = jalaliDateString.split(seprator);
    if (parts.length != 3) {
      throw FormatException("Invalid Jalali date format");
    }

    final int year = int.parse(parts[0]);
    final int month = int.parse(parts[1]);
    final int day = int.parse(parts[2]);

    // Create a Jalali instance
    Jalali jalaliDate = Jalali(year, month, day);

    // Convert Jalali date to DateTime
    DateTime dateTime = jalaliDate.toDateTime();

    // Format DateTime as a string, e.g., "yyyy-mm-dd"
    return '${dateTime.year.toString().padLeft(4, '0')}/${dateTime.month.toString().padLeft(2, '0')}/${dateTime.day.toString().padLeft(2, '0')}';
  } catch (e) {
    print("Error converting Jalali date: $e");
    return null; // Return null in case of error
  }
}