Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Lodash _.isDate ?
Message
 
 
À
Tous
Information générale
Forum:
Javascript
Catégorie:
Codage, syntaxe et commandes
Titre:
Lodash _.isDate ?
Divers
Thread ID:
01638595
Message ID:
01638595
Vues:
52
UPDATE. It looks like I should use moment for the check as defined in this answer:

http://stackoverflow.com/questions/7445328/check-if-a-string-is-a-date-value


Hi everybody,

This is the original code written by my colleague
if ((typeof val === "object") && (val !== null)) {
                        if (val instanceof Date) {
                            var yyyy = val.getFullYear().toString();
                            var mm = (val.getMonth() + 1).toString(); // getMonth() is zero-based
                            var dd = val.getDate().toString();
                            var HH = val.getHours().toString();
                            var MM = val.getMinutes().toString();
                            var SS = val.getSeconds().toString();
                            jsonval.value = (mm[1] ? mm : "0" + mm[0]) + '/' + (dd[1] ? dd : "0" + dd[0]) + '/' + yyyy + ' ' + (HH[1] ? HH : "0" + HH[0]) + ':' + (MM[1] ? MM : "0" + MM[0]) + ':' + (SS[1] ? SS : "0" + SS[0]); // padding
                        }
                        else {
                            jsonval = val;
                        }
                    }
                    else {
                        jsonval = JSON.parse(val);
                    }
and this is a re-write by another colleague
if (_.isDate(val)) {
                jsonval.value = moment(val).format("MM/DD/YYYY HH:mm:ss");
            }

            else if (_.isObjectLike(val)) {
                jsonval = val;
            }

            else {
                jsonval = JSON.parse(val);
            }
which doesn't work. The val is passed as "7/10/2010" but the first check and the second check return false and then JSON.parse produces an error. Do you think instanceof is better to use?

Thanks in advance.
If it's not broken, fix it until it is.


My Blog
Répondre
Fil
Voir

Click here to load this message in the networking platform