locked
How can I get the day, month and year of a datepicker-control? RRS feed

  • Question

  • Hi,

    this is my scenario: I have a datepicker-control and I want to get the selected day, month and year. I tried so many options but it doesn't work.

    This is my datepicker-element:

    <div id ="datePicker" data-win-control="WinJS.UI.DatePicker"></div>

    But I need the code to get the selected day, month and year.

    Can anybody help me?


    Thursday, August 30, 2012 5:33 PM

Answers

  • Get that value contained in the control, don't get the control itself!

    Your code is saying:  Find the control and construct a Date object from it.

    Your code should:  Find the control and get the value from it, put that value in the Date object when you construct it.

    Make sense?

    -Jeff


    Jeff Sanders (MSFT)

    • Marked as answer by Headhunter_X Friday, August 31, 2012 7:43 PM
    Friday, August 31, 2012 5:04 PM
    Moderator
  • Hi Jeff,

    yes it makes sense ;) Now it works.

    this.datePicker = document.getElementById("datePicker");
                this.datePicker.addEventListener("change", function () {
                    day = datePicker.current.getDate();
                    month = datePicker.current.getMonth() + 1;
                    year = datePicker.current.getFullYear();
                }); 
    Thanks.


    • Marked as answer by Headhunter_X Friday, August 31, 2012 7:43 PM
    • Edited by Headhunter_X Friday, August 31, 2012 7:43 PM
    Friday, August 31, 2012 7:42 PM

All replies

  • Put the value in the Date object: http://msdn.microsoft.com/en-us/library/windows/apps/cd9w2te4.aspx

    and extract the values you want.

    -Jeff


    Jeff Sanders (MSFT)

    Thursday, August 30, 2012 6:15 PM
    Moderator
  • Hi Jeff,

    thanks for your answer. The extractions works when I try it with a normal Date-Object:

    var myDate = new Date();

    But when I try to get the selected values of my datepicker-win-control, I get the message NaN because "the object does not represent a specific instant of time". On of my attempts was the following JavScript-Code:

    this.datePicker = element.querySelector("#datePicker");
        var datepickerControl = new WinJS.UI.DatePicker(this.datePicker);
                datepickerControl.element.addEventListener("change", function () {
        var dat = new Date(datepickerControl);
        var dialog = new Windows.UI.Popups.MessageDialog(dat.getFullYear().toString());
                    dialog.showAsync();
    
                });
    But it doesn't work. I'm at an impasse.

    Thursday, August 30, 2012 7:19 PM
  • Get that value contained in the control, don't get the control itself!

    Your code is saying:  Find the control and construct a Date object from it.

    Your code should:  Find the control and get the value from it, put that value in the Date object when you construct it.

    Make sense?

    -Jeff


    Jeff Sanders (MSFT)

    • Marked as answer by Headhunter_X Friday, August 31, 2012 7:43 PM
    Friday, August 31, 2012 5:04 PM
    Moderator
  • Hi Jeff,

    yes it makes sense ;) Now it works.

    this.datePicker = document.getElementById("datePicker");
                this.datePicker.addEventListener("change", function () {
                    day = datePicker.current.getDate();
                    month = datePicker.current.getMonth() + 1;
                    year = datePicker.current.getFullYear();
                }); 
    Thanks.


    • Marked as answer by Headhunter_X Friday, August 31, 2012 7:43 PM
    • Edited by Headhunter_X Friday, August 31, 2012 7:43 PM
    Friday, August 31, 2012 7:42 PM
  • Beautiful my friend!

    Jeff Sanders (MSFT)

    Friday, August 31, 2012 8:05 PM
    Moderator
  • I had to use winControl to get the date etc as in:

    var day = datePicker.winControl.current.getDate()

    HTH


    Phil

    Monday, January 6, 2014 5:01 PM