Created
December 4, 2017 20:22
-
-
Save milon120203/35b8b025fb5190a9ed1163279905ecfd to your computer and use it in GitHub Desktop.
Titanium.UI.Picker for Date with Time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var textfields_width = 300; | |
var textfields_height = 80; | |
var textfields_top = 80; | |
// Boolean flag in case you want to prevent | |
// any of the pickers to show immediately | |
var initialLoad = true; | |
var main_container = Titanium.UI.createWindow({}); | |
var date_container = Ti.UI.createView({ | |
layout:'horizontal', | |
top:0, | |
width:textfields_width, | |
height:Ti.UI.SIZE | |
}); | |
var datePicker = Ti.UI.createPicker({ | |
type: Ti.UI.PICKER_TYPE_DATE | |
}); | |
var date = Ti.UI.createTextField({ | |
editable: false, | |
width:textfields_width/2, | |
height: textfields_height, | |
hintText:"Date", | |
top:textfields_top+35 | |
}); | |
var timePicker = Ti.UI.createPicker({ | |
type: Ti.UI.PICKER_TYPE_TIME, | |
//make sure the time picker is not showing when you add it | |
top: -1000, | |
left: -1000 | |
}); | |
var time = Ti.UI.createTextField({ | |
editable: false, | |
width:textfields_width/2, | |
height: textfields_height, | |
hintText:"Time", | |
top:textfields_top+35 | |
}); | |
//add the time picker to the main container | |
main_container.add(timePicker); | |
date_container.add(date); | |
date_container.add(time); | |
main_container.add(date_container); | |
main_container.open(); | |
datePickerListener = function(e) { | |
//guarding if the focus is on the textfield | |
if (!initialLoad) { | |
datePicker.showDatePickerDialog({ | |
value : new Date(), // some date | |
maxDate : new Date(), | |
callback : function(e) { | |
if (e.value) { | |
date.value = String.formatDate(e.value, 'medium'); | |
} | |
} | |
}); | |
} | |
initialLoad = false; | |
} | |
timePikcerListener = function(e) { | |
timePicker.showTimePickerDialog({ | |
format24: true, | |
callback : function(e) { | |
if (e.value) { | |
time.value = String.formatTime(e.value, 'medium'); | |
} | |
} | |
}); | |
} | |
date.addEventListener('focus', datePickerListener); | |
date.addEventListener('click', datePickerListener); | |
time.addEventListener('focus', timePikcerListener); | |
time.addEventListener('click', timePikcerListener); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment