[TIMOB-26044] Android: Time picker "value" property does not retain assigned year/month/day like iOS
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | Medium |
Status | Open |
Resolution | Unresolved |
Affected Version/s | n/a |
Fix Version/s | n/a |
Components | Android |
Labels | android, engReviewed, parity, picker |
Reporter | Joshua Quick |
Assignee | Joshua Quick |
Created | 2018-05-16T01:39:25.000+0000 |
Updated | 2019-05-21T16:55:06.000+0000 |
Description
{color:red}After thinking about it, perhaps this behavior is okay. If you want the year/month/day parts preserved, then you should be using the
Ti.UI.PICKER_TYPE_DATE_AND_TIME
type. Especially since one problem with preserving the date in a Ti.UI.PICKER_TYPE_TIME
is it has a daylight savings time issue where 2 AM cannot be selected on a spring-forward day (such as 2018/03/11), which is currently the case for both Android and iOS. This behavior would be okay in a date-time picker, but perhaps not in a time-only picker.{color}
*Summary:*
A Ti.UI.Picker
of type PICKER_TYPE_TIME
does not retain the originally assigned year, month, day, and seconds assigned to its value
property. The date returned by value
is always set to the current date (this is wrong), with the hour and minute components set to what's selected in the picker (this last part is fine).
On iOS, all other date components (year, month, day, seconds) are preserved. Android should do the same.
*Test:*
Build and run the below code on Android.
Tap the "Get Selected Time" button.
An alert dialog displays the picker's current date and time. Note that the date is set to current day instead of the hardcoded 2018/01/01.
var window = Ti.UI.createWindow();
var picker = Ti.UI.createPicker({
type: Ti.UI.PICKER_TYPE_TIME,
value: new Date(2018, 0, 1, 12),
});
window.add(picker);
var button = Ti.UI.createButton({
title: "Get Selected Time Value",
bottom: "20dp",
});
button.addEventListener("click", function(e) {
var date = picker.value;
var dialog = Ti.UI.createAlertDialog({
title: "Selected Time",
message: date.toString(),
});
dialog.show();
});
window.add(button);
window.open();
*Note:*
This is an issue with Titanium's "TiUITimeSpinner" Java class...
[TiUITimeSpinner.java](https://github.com/appcelerator/titanium_mobile/blob/master/android/modules/ui/src/java/ti/modules/titanium/ui/widget/picker/TiUITimeSpinner.java)
*Work-Around:*
You can extract the hour and minute selected from the picker and recreate a date object with the correct time like this...
var originalDate = new Date(2018, 0, 1, 12);
picker.value = originalDate;
picker.addEventListener("change", function(e) {
var newDate = new Date(originalDate.getTime());
newDate.setHours(picker.value.getHours());
newDate.setMinutes(picker.value.getMinutes());
});
No comments