Formatting DateTime as RFC-3339 for RSS Feed - date

When I print my DateTime with myDate.toIso8601String();
it prints
2015-11-15T11:55:32.250
Which doesn't validate with an online validator I tried. The spec says it should be of one of the following formats:
2002-10-02T10:00:00-05:00
2002-10-02T15:00:00Z
2002-10-02T15:00:00.05Z
I could try adding a Z at the end, but that seems hacky. Is there a way to print a DateTime in Dart so it complies to one of the following formats?

Z is for UTC. You get it if you convert your DateTime to an UTC time with
new DateTime().now().toUtc().toIso8601String();
Try it in DartPad

Related

Flutter: Display local datetimes correctly

let's say I have a DateTime object and I want to display it in the correct local format.
If I do the following on a German device I get this:
dateTime.toLocal().toString()
// Prints
2022-05-28 23:29:19.518
However, I would expect or desire more something like this for a German device: 28.5.2022 23:29:19
I know that I can format the DateTime but that would just be hardcoding it for a certain locale.
Weirdly enough all the solutions that I found for this on StackOverflow are either hardcoding the format or only apply to Dart, not Flutter.
What is the correct way to display a local datetime in Flutter?
You can use this package intl and localise dates like
var format = DateFormat.yMd('ar');
var dateString = format.format(DateTime.now());
Using the intl package which was mentioned here already, this has been working well for me so far:
DateFormat dateTimeFormat = DateFormat.jm(Localizations.localeOf(context).toString());
DateTime dt = DateTime.fromMicrosecondsSinceEpoch(entity.syncDateTime);
dateTimeFormat.format(dt);
To get outputs which are not yet supported I, for example, concat a ymd formatted DateTime string with a jm formatted DateTime string.

How can I get only date and time value from timestamp value in flutter

I am developing a Flutter project and I want to get 'timestamp' data from firestore and display that value using 'yyyy-MM-dd hh:mm' format. How can I do it ?
Firestore will give you back a Timestamp object as I think you've figured out. It has a method .toDate() which will return a dart DateTime object.
Once you have the Date object, then you should use the DateFormat class as someone so helpfully pointed out in a comment without even linking to the documentation.
That would look something like this:
DateTime date = timestamp.toDate();
DateFormat(yyyy-MM-dd hh:mm").format(date);
although I haven't tested out the format function and dartpad doesn't seem to support the intl package, so you may need to play around with the format codes a bit.

using DateFormat to produce a "2016-12-28T17:43:47.345Z"

I have been looking at the above question and have most of it correct.
I am going to get a datetime in Zulu, and then will want to output that format.
My first go is just as simple as:
DateFormat format = new DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
My issue I am having is the T and Z. Obviously T is used to separate the date from the time and the Z is representative of Zulu time.
That being said the users will be entering a datetime in Zulu, so it wont need to be converted from Local to Zulu, so i was not sure if 'Z' is an acceptable result. I was not sure if there is a different want to handle this, or if my result was the best answer.
Try this package, Jiffy.
String isoFomart = Jiffy().format(); // This will return ISO format from now
You can also add your DateTime object
String isoFomart = Jiffy(DateTime.now()).format(); // This will also return ISO format from now
Hope this helped
The DateTime object has a method called: toIso8601String which is used to return an ISO formatted string. The 'Z' will be added if isUTC is true, otherwise the result will not have the Z in it.
Make sure that the DateTime object itself is correctly set to UTC as if you look in the constructor for the class will tell you a lot of the defaults are local with the exception of the DateTime.utc() static function.
In that concept, you dont really need a DateFormat use to define an iso string.

converting a utc format date string to date object in Extjs

I have a date string in the format "2013-01-31T10:10:05.000Z". I want to convert this string to a Date object in extjs.
I have tried to use Ext.Date.parse("2013-01-31T10:10:05.000Z","Y-m-dTH:i:s.uZ"). But it is returning undefined.
I also tried with new Date("2013-01-31T10:10:05.000Z"), but it is also returning undefined.
Note: I have tried in IE8 browser.
Could anyone please help me to convert the above date string to Date object?
Thanks a lot sra. Now I am getting the result as ...UTC+5:30... Is there any way to convert this in IST format?
Try Ext.Date.parse("2013-01-31T10:10:05.000Z","c");
The c is the format type for ISO 8601 formatted dates
See the Ext.Date API for more details on this or other available formats
That's because 'T' and 'Z' are special characters in the Date format: http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.Date
You have to escape them like this: Ext.Date.parse("2013-01-31T10:10:05.000Z","Y-m-d\\TH:i:s.u\\Z")

Getting a GWT timestamp in UTC format

I am trying to get the current timestamp and convert it into a UTC date for an XML file.
I am using this
import java.util.Date;
import java.util.TimeZone;
import com.google.gwt.i18n.client.DateTimeFormat;
DateTimeFormat.format( new Date(), TimeZone.getTimeZone("UTC"));
but am getting the following error
The method format(Date, TimeZone) in
the type DateTimeFormat is not
applicable for the arguments (Date,
TimeZone)
I need the output as "yyyy-mm-ddThh:mm:ssZ"
Use com.google.gwt.i18n.client.TimeZone.createTimeZone(0) to create a UTC TimeZone object, and then use that in DateTimeFormat.format(Date, TimeZone).
You can use apostrophe to indicate literals in a DateTimeFormat pattern.
eg. "HH'o''clock'"
So, the formatter you need would look something like this:
DateTimeFormat formatter = DateTimeFormat.getFormat("yyyy-mm-dd'T'HH:mm:ssZ");
I tried it out. It gave me an output in format 2010-16-29T08:16:23+0530
Is this what you are looking for?
You should provide a com.google.gwt.i18n.client.TimeZone instead of java.util.TimeZone