3

I have an input text that has a combination of date and time and display like this

04/01/2015 8:48PM 

How can i convert this string to a date using the function new Date() in javascript? not output is shown

Here is what i've tried so far, i can only convert the date not the time. HTML

 <form name="frm1" >


      <h3>Check in Date:</h3>
      <input type="text" value="" class="datetimepicker_mask" name="dtp1" /><br><br>

      <h3>Check out Date:</h3>
      <input type="text" value="" class="datetimepicker_mask" name="dtp2" /><br><br>

      <input type="button" onclick="computeDate()" value="Compute Difference" /> 

      <br><b>No of days: </b>
      <span id="date_difference"></span>

     </form>

JAVSCRIPT

function computeDate() {

        var dateTime1 = document.frm1.dtp1.value;
        var dateTime2 = document.frm1.dtp2.value;


        var startDate = new Date(dateTime1);
        var endDate = new Date(dateTime2);




        var timeDiff = Math.abs(endDate.getTime() - startDate.getTime());

        if (timeDiff == 0) {
            timeDiff = 1;
        }

        var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 

        var total = parseFloat(diffDays) * parseFloat(roomRate);

        document.getElementById("date_difference").innerHTML = diffDays;
        document.getElementById("date_difference").style.visibility = "visible";

    }

3 Answers3

1

If the date format is always the same, create a convience function that converts the date to a Date object

function convert(date) {
    var dateArr = date.split(/[\s\/\:]/);

    if (dateArr[4].toLowerCase().indexOf('pm') != -1) 
        dateArr[3] = (+dateArr[3]) + 12;

    dateArr[4] = dateArr[4].replace(/\D/g,'');
    dateArr[0]--;

    return new Date(dateArr[2], dateArr[0], dateArr[1], dateArr[3], dateArr[4]);
}

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
0

Here is an answer that will both solve this and make development easier. This suggestion will require an extra library for addressing such issues as you are having here- time, but you'll likely find it beneficial when working with JavaScript dates in general. It already looks like you're writing manual date functions. Abstract them away with robust libraries for solving these same issues that have come up again and again. Using date.js, here is how easy this becomes

Date.parse('04/01/2015 8:48PM ')

JSFiddle Example

scniro
  • 16,844
  • 8
  • 62
  • 106
0

You can create the Date object after parsing the dateString

var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);

you can use the parseDate function as following

var testDate = "04/01/2015 8:48PM";
console.log(parseDate(testDate));

function parseDate(dateStr){
    var dateTime = dateStr.split(/\/| |:|(?=[PA])/);

    for(var i=0; i<5; i++){
        dateTime[i] = parseInt(dateTime[i]);
    }
    if(dateTime[5] == "PM"){
        dateTime[3] += 12;
    }

    return new Date(dateTime[2], dateTime[1], dateTime[0], dateTime[3], dateTime[4]);
}

Try it at JSFiddle

gokaka
  • 136
  • 1
  • 5