John Joe
John Joe

Reputation: 12803

Convert String to DateTime in flutter

I have this code

if (obj.due_date != null) {
      print('The date is  '+obj.due_date);
      print('now change to ' +
       DateUtil().formattedDate(DateTime.parse(obj.due_date)));
 }

DateUtil

import 'package:intl/intl.dart';

class DateUtil {
  static const DATE_FORMAT = 'dd/MM/yyyy';
  String formattedDate(DateTime dateTime) {
    print('dateTime ($dateTime)');
    return DateFormat(DATE_FORMAT).format(dateTime);
  }
}

My output become this

I/flutter ( 5209): The date is 2019-11-20T00:00:00.000+08:00

I/flutter ( 5209): dateTime (2019-11-19 16:00:00.000Z)

I/flutter ( 5209): now change to 19/11/2019

Why it will change from 20 to 19?

Upvotes: 3

Views: 15022

Answers (2)

Priyansh jain
Priyansh jain

Reputation: 1422

Not all strings can be converted to DateTime. These are not accepted as right:

UNACCEPTED:

18-11-20222
Nov, 20 2022
19/11/2022
12/30/2023

Accepted formats:

  • A date: A signed four-to-six-digit year, two-digit month, and two-digit day, optionally separated by – characters. Examples: “19700101”, “-0004-12-24”, and “81030-04-01”.
  • An optional time part, separated from the date by either T or space. The time part is a two-digit hour, then optionally a two-digit minutes value, then optionally a two-digit seconds value, and then optionally a ‘.’ or ‘,’ followed by at least a one-digit second fraction. The minutes and seconds may be separated from the previous parts by a ‘:’. Examples: “12”, “12:30:24.124”, “12:30:24,124”, “123010.50”.
  • An optional time-zone offset part, possibly separated from the previous by a space. The time zone is either ‘z’ or ‘Z’, or it is a signed two-digit hour part and an optional two-digit minute part. The sign must be either “+” or “-“, and can not be omitted. The minutes may be separated from the hours by a ‘:’. Examples: “Z”, “-10”, “+01:30”, and “+1130”.

ACCEPTED:

"2022-02-27"
"2022-02-27 13:27:00"
"2022-02-27 13:27:00.123456789z"
"2022-02-27 13:27:00,123456789z"
"20220227 13:27:00"
"20120227T132700"
"20220227"
"+20220227"
"2002-02-27T14Z"
"2002-02-27T14+00:00"
"-123450101 00:00:00 Z": in the year -12345.
"2002-02-27T14:00:00-0500": Same as "2002-02-27T19:00:00Z"

Example:

void main() {
  const String input1 = '2012-02-27 13:27:00.123456789z'; // this is valid
  const String input2 = '2002-02-27T14+00:00'; // this is valid
  const String input3 = "2022 Feb 12"; // This will not be accepted

  final dt1 = DateTime.tryParse(input1);
  print(dt1);

  final dt2 = DateTime.tryParse(input2);
  print(dt2);

  final dt3 = DateTime.tryParse(input3);
  print(dt3);
}

Instead of using DateTime.tryParse(), you can also use DateTime.parse() but this will throw a FormatException if the string passed is in incorrect format. In case of tryParse() it will throw a null value if string is incorrect.

Upvotes: 0

Nagaraj Alagusundaram
Nagaraj Alagusundaram

Reputation: 2459

It is because of the value that you hold in

obj.due_date

As per the log, the current value is

2019-11-20T00:00:00.000+08:00

When I used the below code

var tempDate = '2019-11-20T00:00:00.000+00:00';
print('The date is  '+tempDate);
print('now change to ' +DateUtil().formattedDate(DateTime.parse(tempDate)));

the logs are as follows:

I/flutter (18268): The date is  2019-11-20T00:00:00.000+00:00
I/flutter (18268): dateTime (2019-11-20 00:00:00.000Z)
I/flutter (18268): now change to 20/11/2019

The only change between these codes is the value that we pass.

2019-11-20T00:00:00.000+00:00

It is purely a timezone issue.

Try below code

var tempDate = DateTime.now().toLocal().toString();

Log show

I/flutter (18268): 2019-11-15 16:06:54.786814
I/flutter (18268): The date is  2019-11-15 16:06:54.787186
I/flutter (18268): dateTime (2019-11-15 16:06:54.787186)
I/flutter (18268): now change to 15/11/2019

Likewise, when you use the following code

var tempDate = DateTime.now().toUtc().toString();

the logs are as follows:

I/flutter (18268): 2019-11-15 16:07:35.078897 
I/flutter (18268): The date is  2019-11-15 05:07:35.079251Z 
I/flutter (18268): dateTime (2019-11-15 05:07:35.079251Z) 
I/flutter (18268): now change to 15/11/2019

Hence the final answer is, change below line

DateUtil().formattedDate(DateTime.parse(tempDate)));

to

DateUtil().formattedDate(DateTime.parse(tempDate).toLocal())

Upvotes: 6

Related Questions