Why does ExtJS subtract a day when formatting a date? - date

Using ExtJS 4.0.2, I can type the following into the console:
Ext.util.Format.date('2012-01-13', "m-d-Y");
I get 01-12-2012
Why?
I can correct it with:
Ext.util.Format.date('2012-01-13 00:00:00', "m-d-Y");

Ext.util.Format.date in Ext 4.0.2 uses a Date object or a String (your case). This string is parsed using the native Date.parse() using the UTC time zone.
Try to explicitly parse it using Ext.Date.parse:
var dt = Ext.Date.parse("2012-01-13", "Y-m-d");
Ext.util.Format.date(dt, "m-d-Y");

This problem exists in Ext3, but the solution is slightly different:
var dt = '2012-01-31'; //date string
dt = Date.parseDate(dt, "Y-m-d");
Ext.util.Format.date(dt, 'm/d/Y'); //returns 01/31/2012

If you're unable to use Gregor's answer (e.g. filling a grid), note that changing the input to a non ISO 8601 date format will avoid the UTC parsing as well. For example
Ext.util.Format.date('01/13/2012', "Y-m-d");
will give 2012-01-13

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.

Format string date into date object

I have a date in this format: December 5th 2017. How do I convert it to either a timestamp or a date object?
I tried the following but it return NaN:
let tDate = new Date('December 5th 2017')
use a library like moment.js (momentjs.com) to parse custom date formats into Dates. As of version 2.13 it apparently comes with typings out of the box and can therefore be used easily with TypeScript (aureliajsrocks.com/moment-js-now-typescript-compatbile)

Date display Appery.io

I use a date picker that saves the date in a yyyy-mm-dd format in a database. It then automatically adds time that always appears as 00:00:00. So for example it displays the date like this : 2014-12-14 00:00:00. I want it to display just the date in a mm-dd-yyyy format.
I use the code below but something seems to be wrong with it because it simply doesn't change the way it is displayed. I want to split up each of the values, then display only the date in a different format.
var parts = value.split("/");
return parts[1] + "-" +parts[2] + "-" +parts[0];
What can I do to make it work? Javascript please.
Thanks in advance.
It's a wide variety of solutions. Depends on you server-side settings too, but I will assume, that you use common for most websites MySQL DB and PHP.
You can change you column type to DATE. As said in docs:
The DATE type is used for values with a date part but no time part.
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The
supported range is '1000-01-01' to '9999-12-31'.
You can change your column type to VARCHAR and store a date in any format you like when INSERT it
You can convert it to proper format using MySQL built-in DATE_FORMAT()
SELECT DATE_FORMAT('2014-12-14 00:00:00', '%c-%e-%Y');
will give you 12-14-2014. Here is sqlfiddle,
just change first param to your column name in query
On server you can use PHP's date() function like this
$yourdate = '2014-12-14 00:00:00';
echo date("m-d-Y", strtotime($yourdate));
Closer to your question, how to do it in JS? It also has special object for this: Date(). Put your date in constructor, then use methods:
var yourdate = new Date('2014-12-14 00:00:00');
alert(yourdate.getMonth() + "-" + yourdate.getDate() + "-" + yourdate.getFullYear());
JSFiddle < - here
So, as far as almost every platform understands this datetime format - you can use any tool for converting it

Converting a string timestamp to Date results in reset to UNIX epoch

I'm having some problems converting a string to a date object in google apps script.
My dates are in the following format, from a 3rd party API:
2013-01-17T17:34:50.507
I am attempting to convert this to a Date object:
return Date(stringDate);
And this is being returned:
Thu Jan 01 01:00:00 GMT+01:00 1970
Can someone tell me what i'm doing wrong, and how to resolve this issue ?
With moment.js, it is as easy as this to parse any of ISO 8601 format.
var date = Moment.moment("2013-01-17T17:34:50.507").toDate();
You can use moment.js to parse your arbitrary date string as well.
To use moment.js in GAS, you just need to add it in the script editor.
Open your script in GAS script editor and go to "Resources" then "Libraries...", then put this project key MHMchiX6c1bwSqGM1PZiW_PxhMjh3Sh48 and click "Add". Choose the version from the dropdown, then click "Save". Now, you are ready to use moment.js in GAS.
moment.js can be used to parse date string, create formatted date string, and many other date manipulation. Thanks to the author!
You can find the moment.js documentation here.
It doesn't appear that the Date object knows how to handle that date. The date is in ISO 8601 format. Javascript can handle Dates if they are given timezone information.
You will have to do some testing, but if those dates given to you are in UTC time, then just add a Z to the end of the date string before you call new Date().
Edit: The Apps Script Date object can't seem to handle a timezone other than UTC when parsing a Date. I opened an issue for it.
It doesn't work in GScript, at least for me at the time I'm writing it.
This post serves a working alternative: How do I format this date string so that google scripts recognizes it?
As of now new Date() seems to work:
var dT = new Date("2013-01-17T17:34:50.507");
console.info("dT: %s or %d", dT, dT.getTime());
returns dT: Thu Jan 17 17:34:50 GMT+01:00 2013 or 1.358440490507E12 in Google Apps Script

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")