Change date format string by a function and string replace - Javascript Date Operation

Javascript examples for Date Operation:Date Format

Description

Change date format string by a function and string replace

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){/*  w  w  w. j a va  2 s . co  m*/
    function validateDateFormat(s)
    {
        if(s.indexOf('yyyy') > -1)
            return s.replace('yyyy','yy');
        return s.replace('yy','y');
    }
console.log(validateDateFormat('dd/mm/yyyy')); // "dd/mm/yy"
console.log(validateDateFormat('dd/mm/yy')); // "dd/mm/y"
    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials