locked
DatePicker control in HTML5

    Question

  • Hi  all,

    I am trying to use DatePicker Control in one of my Windows Store application. The DatePicker control is showing me the value in UTC format like:-

    Mon Feb 3 12:00:00 UTC +05:30 2014

    The code I have tried to use is:-

    datePickerControl = document.getElementById('datepickeradd').winControl;
             datePickerControl.current = new Date(); //The Current Date
            
             selectedDate = datePickerControl.current;
             convertedDate = datePickerControl.current;
             convertedDate = convertedDate.toLocaleDateString();

    When I used "toLocaleDateString()" method then it gives me the output as :- "Monday,February 3,2014"

    But I want the date in the format "2014/02/03". I do not know how to proceed with this. What is the way of formating the Date of the DatePicker Control to "YYYY/MM/DD" Format.

    Please help me in resolving this issue. I have searched the net but did not find any helpful link.

    Thanks,

    Vishwajeet

    Monday, February 3, 2014 6:12 AM

Answers

  • Sorry, I think there is no in-build JavaScript function for you to convert to a date format YYYY/MM/DD, and my fault, I misread the format in my last post.

    You should be able to convert the date by yourself, try following code:

    Date.prototype.toMyString = function () {
     
        function padZero(obj) {
              obj = obj + '';
              if (obj.length == 1)
                  obj = "0" + obj
              return obj;
        }
        
        var output = "";
        output += this.getFullYear();
        output += "/";
        output += padZero(this.getMonth()+1);
        output += "/";
        output += padZero(this.getDate());
        
        return output; 
    }
        
    var d = new Date();
    var myDate = d.toMyString();
    // should be able to see 2014/02/04

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, February 4, 2014 1:24 AM
    Moderator

All replies

  • Hi Vishwajeet,

    By using DatePicker.current we can get the Date() object, and see the function name "toLocaleDateString" means the output will follow your PC setting (http://www.w3schools.com/jsref/jsref_tolocaledatestring.asp).

    The possible solution is convertedDate = convertedDate.toLocaleDateString("en-US");

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, February 3, 2014 8:03 AM
    Moderator
  • Thanks a lot for reply & your support.

    Even after using convertedDate = convertedDate.toLocaleDateString("en-US");

    in my code. I am getting the same output.

    What I want is just the date in formate "YYYY/MM/DD"

    Thanks,

    Monday, February 3, 2014 9:35 AM
  • Sorry, I think there is no in-build JavaScript function for you to convert to a date format YYYY/MM/DD, and my fault, I misread the format in my last post.

    You should be able to convert the date by yourself, try following code:

    Date.prototype.toMyString = function () {
     
        function padZero(obj) {
              obj = obj + '';
              if (obj.length == 1)
                  obj = "0" + obj
              return obj;
        }
        
        var output = "";
        output += this.getFullYear();
        output += "/";
        output += padZero(this.getMonth()+1);
        output += "/";
        output += padZero(this.getDate());
        
        return output; 
    }
        
    var d = new Date();
    var myDate = d.toMyString();
    // should be able to see 2014/02/04

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, February 4, 2014 1:24 AM
    Moderator
  • Thanks a lot. It helped me in Getting the date in YYYY/MM/DD format.

    Similar approach I used for this purpose.

    datePickerControl = document.getElementById('datepickeradd').winControl;
    datePickerControl.current = new Date();
                   
    var currentDate = new Date(datePickerControl.current);

    var twoDigitMonth = ((currentDate.getMonth() + 1) >= 10) ? (currentDate.getMonth() + 1) : '0' + (currentDate.getMonth() + 1);

    var twoDigitDate = ((currentDate.getDate()) >= 10) ? (currentDate.getDate()) : '0' + (currentDate.getDate());

    var year = currentDate.getFullYear();

    // String concatenation of all the above retrieved date, month & year

    var   convertedDate = year + "/" + twoDigitMonth + "/" + twoDigitDate;

    The value of "convertedDate" equals date in "YYYY/MM/DD" Format.

    Thanks,

    Tuesday, February 4, 2014 4:39 AM