EmmaLat EmmaLat - 4 months ago 29x
Javascript Question

How to fetch a date from a datepicker on NativeScript

Here is my view :

<Page xmlns="http://www.nativescript.org/tns.xsd" loaded="onPageLoaded">
<DatePicker day="15" month="5" year="2015" verticalAlignment="center" id="date"/>
<Label text="Saisissez une heure : " verticalAlignment="center"/>
<TextField verticalAlignment="center" width="300" hint="Enter text..."/>
<Button text="Valider" tap="see"></Button>

I wanted to know how to get the date from this datepicker i tried that :

var viewModule = require("ui/core/view");
var dialogs = require("ui/dialogs");
var page;

exports.onPageLoaded = function(args) {
page = args.object;

exports.see = function() {
dialogs.alert(viewModule.getViewById( page, "date" ).year).then(function() {


but it did not work.

Thanks for answering.


Try to use the getViewById method on your page variable. Your DatePicker resides inside your page variable which - according to the docs - is a derived type of the View class which contains the getViewById method.

Changing your "see" method like this should probably work (didn't test it myself):

exports.see = function() {
    dialogs.alert(page.getViewById("date").year).then(function() {