5

How do I convert string(09-Apr-2010) in to date time (09 April 2010 00:00:00) using java script? I need to compare the dates for validation.

2 Answers 2

17

check out Date.parse

str = "09-Apr-2010"
date = new Date(Date.parse(str.replace(/-/g, " ")))
alert(date.toLocaleString())
Sign up to request clarification or add additional context in comments.

1 Comment

One caveat: Date.parse with ISO format dates ('2010-04-01') doesn't seem to work on IE7, although it works in Firefox and Chrome.
1

Try this should work

<script language="javascript">
    function validateDate(oSrc, args)
    {
        var iDay, iMonth, iYear;
        var arrValues;
        arrValues = args.Value.split("/");
        iMonth = arrValues[0];
        iDay = arrValues[1];
        iYear = arrValues[2];

        var testDate = new Date(iYear, iMonth - 1, iDay);

        if ((testDate.getDate() != iDay) ||
            (testDate.getMonth() != iMonth - 1) ||
            (testDate.getFullYear() != iYear))
        {
            args.IsValid = false;
            return;
        }

        return true;
    }
</script>

1 Comment

np. I know it's old but I got here via Google so I figured others still may come :)

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.