1

I have been wrestling with this for a couple hours now at least and would really appreciate any insight as to what I'm doing wrong.

I want to compare 2 date objects and need to convert one date that I'm grabbing from a datetime input. when I put the variable being set into a new Date(myVariableHere); it doesn't work, though if I add in the string equivalent new Date(2014-01-05T04:47Z); it works fine (so it seems).

My fiddle here: http://jsfiddle.net/centinel3/Y8kWf/

note: I tried also using moment.js and wasn't able to make it do what I needed it to.

Some code snippets below and thank you in advance.

        // HTML            
        <input id="start-date" type="datetime"/>            

        // Gets value from start date "datetime selectbox"
        var userStartDate = ""; 
        $("#start-date").change(function () {
            userStartDate = $(this).val();
        });

        var startDate = new Date("2014-01-05T04:47Z"); // works
        var startDate = new Date(userStartDate ); // doesn't work

Working/fixed code below (this also adjusts the format so that it plays well in safari:

        var userStartDate, startDate;
        $("#start-date").change(function () {
            userStartDate = ($(this).val()).replace(/-/g, "/").replace("T", " ");
            startDate = new Date(userStartDate);
        });
2
  • Can you do a jsfiddle ? With html :) Commented Jan 5, 2014 at 5:14
  • Here is my fiddle: jsfiddle.net/centinel3/Y8kWf/3 Commented Jan 5, 2014 at 12:47

3 Answers 3

2

You need to add the code that uses userStartDate within the event handler:

    $("#start-date").change(function () {
        startDate = new Date($(this).val());
    });

By setting startDate outside the event handler, nothing happens when the value changes and startDate will only have the original value in it.

If you want something to happen when the user changes the date, that code should go in the event handler, or be called from within it.

Sign up to request clarification or add additional context in comments.

3 Comments

That isn't the issue. Please see my fiddle for what is going on. It's not that the variable isn't being populated, it's that it doesn't convert to a Date object. My fiddle is here: jsfiddle.net/centinel3/Y8kWf/3
@Centinel3 I see. Just replace userStartDate = $(this).val(); with startDate = new Date($(this).val()); and it looks like it works fine. I've updated my answer above.
Thank you for your help! I chose another response that was a little more descriptive and also declared the variables outside the event handler. you were also correct. Thanks again!
0

Just update the following lines:

var userStartDate = "";
$("#start-date").change(function () {
    userStartDate = $(this).val();
});

var startDate = new Date(userStartDate);

TO:

var userStartDate = "", startDate;

$("#start-date").change(function () {
    userStartDate = $(this).val();
    startDate = new Date(userStartDate); //so each time it gets the updated time;
});

1 Comment

This works! Thanks man. I had to also refactor a bit due to the format not working in Safari. I adjusted this post to reflect that. Thanks again! -Chris
0

You need to define the startDate variable inside the "change" event. Because if you do it outside the change event would probably fire after you define the variable – hence it gets the value undefined.

    var userStartDate, startDate; 

    $("#start-date").change(function () {
        userStartDate = $(this).val();
        startDate = new Date(userStartDate);
    });

1 Comment

You had the same answer as the developer who posted just a hare ahead of you. Thanks for the response!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.