0
function convertDateTimeFromUTCtoLocalForTable() {
    $("td[data-ConvertUTCToLocal]").each(function (i, el) {
        if (el.innerHTML) {
            var localDate = new Date(el.innerHTML + ' UTC');
            el.innerHTML = localDate.toString("MM/dd/yyyy HH:mm"); //Method in data.js file  , Remote URL http://www.datejs.com/build/date.js
        }
    });
    $("span[data-ConvertUTCToLocal]").each(function (i, el) {
        var localDate = new Date(el.innerHTML + ' UTC');
        el.innerHTML = localDate.toString("MM/dd/yyyy HH:mm"); //Method in data.js file  , Remote URL http://www.datejs.com/build/date.js
    });
}

I am manipulating dates at run time. I need to check if the expresion returns a valid date before i pass it to .toString method.

Eg

    if(IsvalidDate(localDate){
//Set value
    }
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Shanker Paudel
  • 740
  • 1
  • 9
  • 21
  • 1
    first define "valid date" - the JS Date parsing functions only formally accept a few limited formats - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse – Alnitak Jul 07 '14 at 11:39
  • 1
    `if ( !isNaN(localDate) )` will do – Bergi Jul 07 '14 at 11:39
  • Note that `el.innerHTML + ' UTC'` does not produce a string that is consistent with that specified for [*Date.parse* in ES5](http://ecma-international.org/ecma-262/5.1/#sec-15.9.1.15), so you are relying on inconsistent browser parsing for your dates. The fact that the result of `Date.parse(…)` is a valid Date object does not necessarily mean that the input string was or wasn't a valid date. – RobG Jul 07 '14 at 11:44
  • @Bergi: I would suggest to promote your comment to answer. Otherwise this question will stay marked as 'unanswered'. And you deserve the credits. – Frank Conijn - Support Ukraine Jul 07 '14 at 11:46
  • @FrankConijn: There's a better solution to that problem :-) – Bergi Jul 07 '14 at 11:50
  • @Bergi and what is it? – Shanker Paudel Jul 07 '14 at 11:52
  • 1
    @ShankerPaudel: Just closing the question as a duplicate. I don't need reputation from replicating existing answers :-) – Bergi Jul 07 '14 at 11:56

0 Answers0