getDateTime static method

DateTime getDateTime({
  1. String formartDate = "1970-10-01 00:00:00",
})

Implementation

static DateTime getDateTime({String formartDate = "1970-10-01 00:00:00"}) {
  if (formartDate.isEmpty) {
    return DateTime.now();
  }

  //年必须大于1970年
  String mYear = formartDate.substring(0, 4);
  if (int.parse(mYear) < 1970) {
    mYear = "1970";
  }
  var result;
  try {
    result = mYear +
        "-" +
        formartDate.substring(5, 7) +
        "-" +
        formartDate.substring(8, 10);
    if (formartDate.toString().length >= 13 &&
        formartDate.substring(10, 13).isNotEmpty) {
      result += "" + formartDate.substring(10, 13);
    }
    if (formartDate.toString().length >= 17 &&
        formartDate.toString().substring(14, 16).isNotEmpty) {
      result += ":" + formartDate.substring(14, 16);
    }
    if (formartDate.toString().length >= 19 &&
        formartDate.substring(17, 19).isNotEmpty) {
      result += ":" + formartDate.substring(17, 19);
    }
    var dateTime = DateTime.parse(result);
    return dateTime;
  } catch (e) {
    throw e;
  }
}