Reputation: 2748
What type of date format is this?
2020-03-26T00:57:08.000+08:00
I'm using DateFormat class
DateTime dateTime = DateTime.now();
print(dateTime.toIso8601String());
print(dateTime.toLocal());
print(dateTime.toUtc());
Output
I/flutter (20667): 2020-03-26T01:34:20.826589
I/flutter (20667): 2020-03-26 01:34:20.826589
I/flutter (20667): 2020-03-25 17:34:20.826589Z
I would like to have a date format like the first output I show, which has the +08:00 behind. Which should I use?
Upvotes: 8
Views: 24109
Reputation: 47
Use the methods toLocal()
and toUtc()
to get the equivalent date/time value specified in the other time zone
read more https://api.flutter.dev/flutter/dart-core/DateTime-class.html
Upvotes: 0
Reputation: 139
I have tried this in flutter
Using package: intl: ^0.17.0
Text(DateFormat.yMMMd().add_jm().format(<your date format for e.g. 2022-09-08T19:20:40.650367+05:30>.toLocal())
Output:
Sep 8, 2022 7:20 PM
Upvotes: -1
Reputation: 17402
What date format is this?
"2020-03-26T00:57:08.000+08:00"
This date-time format follows the RFC 3339 standard, and more generally the ISO 8601 standard. The letter "T" is known as the time designator. The "+08:00" is known as the UTC timezone offset.
I would like to have a date format [...], which has the +08:00 behind
To the date-time, you can append the UTC hour and minute offsets:
// import 'package:intl/intl.dart' as intl show DateFormat;
void main() {
DateTime now = DateTime.now();
Duration offset = now.timeZoneOffset;
// ----------
String dateTime = now.toIso8601String();
// - or -
// String dateTime = intl.DateFormat("yyyy-MM-dd'T'HH:mm:ss").format(now);
// ----------
String utcHourOffset = (offset.isNegative ? '-' : '+') +
offset.inHours.abs().toString().padLeft(2, '0');
String utcMinuteOffset = (offset.inMinutes - offset.inHours * 60)
.toString().padLeft(2, '0');
String dateTimeWithOffset = '$dateTime$utcHourOffset:$utcMinuteOffset';
print(dateTimeWithOffset);
}
I'm using DateFormat class
DateFormat (https://api.flutter.dev/flutter/intl/DateFormat-class.html) does not format a UTC timezone offset. And while the letter "Z" in the documentation appears to provide the UTC timezone offset, it's reserved, and you can not use DateFormat("Z")
as that throws an Unimplemented Error (https://api.flutter.dev/flutter/dart-core/UnimplementedError-class.html). Note that "Z" (pronounced phonetically as "zulu") stands for zero meridian time, and has a UTC timezone offset of +0:00.
Upvotes: 7
Reputation: 107171
There is no direct way of getting that kind of date format as of now. There is a work-around.
import 'package:intl/intl.dart';
var dateTime = DateTime.now();
var val = DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").format(dateTime);
var offset = dateTime.timeZoneOffset;
var hours = offset.inHours > 0 ? offset.inHours : 1; // For fixing divide by 0
if (!offset.isNegative) {
val = val +
"+" +
offset.inHours.toString().padLeft(2, '0') +
":" +
(offset.inMinutes % (hours * 60)).toString().padLeft(2, '0');
} else {
val = val +
"-" +
(-offset.inHours).toString().padLeft(2, '0') +
":" +
(offset.inMinutes % (hours * 60)).toString().padLeft(2, '0');
}
print(val);
Upvotes: 8
Reputation: 2024
What type of date format is this?
This format is UTC + timezone offset.
That +08:00 is the time zone's offset that has already been added.
Seems like DateTime
doesn't contain timezone information, so, you can't create a DateTime in a specific timezone. Only the timezone of your system and UTC are available.
Is important to say also that DateTime
support timezone offset for parsing, but normalizes it to UTC or local time.
So since this is UTC you can probably format it using toUtc
or toLocal
and the receiver will be able to parse it.
With that said, you can simply parse it like this:
DateTime.parse("2020-03-26T00:57:08.000+08:00")
Upvotes: 2