Skip to content

Commit

Permalink
fix(android): date.toLocaleString() to default to numeric date/time
Browse files Browse the repository at this point in the history
  • Loading branch information
jquick-axway authored and sgtcoolguy committed Dec 18, 2020
1 parent 6323c69 commit 436c3a3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
31 changes: 30 additions & 1 deletion common/Resources/ti.internal/extensions/js/Date.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,34 @@ if (OS_ANDROID) {
return formatter.format(this);
};

Date.prototype.toLocaleString = Date.prototype.toLocaleDateString;
Date.prototype.toLocaleString = function () {
const properties = Intl.DateTimeFormat._makeTiCreationPropertiesFrom(arguments);
const oldOptions = properties.options;
let hasOption = false;
if (oldOptions) {
hasOption
= !!oldOptions.dateStyle
|| !!oldOptions.timeStyle
|| !!oldOptions.weekday
|| !!oldOptions.month
|| !!oldOptions.day
|| !!oldOptions.year
|| !!oldOptions.hour
|| !!oldOptions.minute
|| !!oldOptions.second;
}
if (!hasOption) {
const defaultOptions = {
month: 'numeric',
day: 'numeric',
year: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric'
};
properties.options = Object.assign(defaultOptions, oldOptions);
}
const formatter = new Intl.DateTimeFormat(properties.locale, properties.options);
return formatter.format(this);
};
}
14 changes: 8 additions & 6 deletions tests/Resources/date.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const should = require('./utilities/assertions');

describe('Date', function () {
it('#toLocaleString()', () => {
// 2020-March-1st
const date = new Date(2020, 2, 1);
// 2020-March-1st 08:02:05 PM
const date = new Date(2020, 2, 1, 20, 2, 5);
const options = {
year: 'numeric',
month: 'numeric',
Expand All @@ -35,10 +35,12 @@ describe('Date', function () {
should(date.toLocaleString('de-DE', options)).be.eql('1.3.2020');
should(date.toLocaleString('ja-JP', options)).be.eql('2020/3/1');

should(date.toLocaleDateString('en-US')).be.eql('3/1/2020');
should(date.toLocaleDateString('en-US', { year: 'numeric' })).be.eql('2020');
should(date.toLocaleDateString('en-US', { year: '2-digit' })).be.eql('20');
should(date.toLocaleDateString('en-US', { hour: 'numeric', hour12: true })).be.eql('3/1/2020, 12 AM');
should(date.toLocaleString('en-US')).be.equalOneOf([ '3/1/2020, 8:02:05 PM', '3/1/2020, 20:02:05' ]);
should(date.toLocaleString('en-US', { hour12: true })).be.eql('3/1/2020, 8:02:05 PM');
should(date.toLocaleString('en-US', { hour12: false })).be.eql('3/1/2020, 20:02:05');
should(date.toLocaleString('en-US', { year: 'numeric' })).be.eql('2020');
should(date.toLocaleString('en-US', { hour: 'numeric', hour12: true })).be.eql('8 PM');
should(date.toLocaleString('en-US', { weekday: 'long' }).toLowerCase()).be.eql('sunday');
});

it('#toLocaleDateString()', () => {
Expand Down

0 comments on commit 436c3a3

Please sign in to comment.