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?
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With