Convert string to datetime javascript

How can I convert a string (09-Apr-2010) to a date (April 09, 2010 00:00:00) using a java script? I need to compare dates to check.

+5
source share
3 answers

check Date.parse

str = "09-Apr-2010"
date = new Date(Date.parse(str.replace(/-/g, " ")))
alert(date.toLocaleString())
+17
source

Try it, it 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
source

All Articles