0

I have a requirement where i have to filter my data based on datetime.I am retrieving data in server side and passing it to client side by serializing it.My date is in format( "2014-01-01 12:00:00").Controls i used are Telerik RadDatePicker and RadSlider, upon the change of slider value i want to filter my data by adding hours to date selected and comparing it with my data. Is there any easy way to do it in javascript which will handle all the scenarios like handling leap year scenario as the date will change when the hours will exceed 24.

enter image description here

This is what solve my problem.

            function clientValueChange(sender, eventArgs) {

        var hr = parseInt(sender._itemData[sender.get_value()].value);
        if (dtWPSelDate == "")
            return;
        var arr = dtWPSelDate.split(' ')[0].split('-');
        var dtt = new Date();

        dtt.setYear(arr[0]);
        dtt.setMonth(parseInt(arr[1]-1));
        dtt.setDate(arr[2]);

        if (eventArgs.get_oldValue() > eventArgs.get_newValue())
            dtt.setHours(-hr,0,0);
        else
        dtt.setHours(hr,0,0);
        dtt.setMinutes(0);
        dtt.setSeconds(0);

        if ((parseInt(hr) % 24) == 0) {

            dtWPSelDate = dtt.format('yyyy-MM-dd 00:mm:ss');
        }
        else
        dtWPSelDate = dtt.format('yyyy-MM-dd hh:mm:ss');

        console.log(dtWPSelDate);
    }

    var dtWPSelDate ;

    function DateSelected(sender, eventArgs) {
        dtWPSelDate = eventArgs._newValue;


    } 
hampi2017
  • 701
  • 2
  • 13
  • 33

1 Answers1

0
var dateString = "4/10/2014"; //consider this as your date in string DataType
var dateArray = dateString.split("/");
var date = new Date(dateArray[2], Number(dateArray[1])-1, dateArray[0]);
//To add Hours
var hoursToBeAdded = 10;
date.setHours(hoursToBeAdded);

Highlight point is you no need to worry about leap year which will be handled by javascript itself.

Praveen
  • 55,303
  • 33
  • 133
  • 164
  • This doesn't add the hours, it sets the hour to the specified value. I.e. it doesn't add 10 hours, it sets the hour to 10, it doesn't modify minutes or seconds. To add the hours, do `date.setHours(date.getHours() + hoursToBeAdded)`, assuming *hoursToBeAdded* is type number. – RobG Mar 25 '21 at 02:33