www.webdeveloper.com
Results 1 to 7 of 7

Thread: how to convert string in YYMMDD format to DD/MM/YY format

Hybrid View

  1. #1
    Join Date
    Mar 2007
    Posts
    4

    Question how to convert string in YYMMDD format to DD/MM/YY format

    Hi
    I have string in YYMMDD format but i need to convert to DD/MMYY format please let me know is there any built in fuctions available in javascript api

    ex:130122 to 22/01/13
    Please help me

    Thanks
    Srinivasula Reddy

  2. #2
    Join Date
    Aug 2008
    Location
    Sweden
    Posts
    227
    You can use string.substr() to extract parts of a string. Just cut up your date string and put it together in the order you want.
    New to web development or in need of a good reference? Check out the Mozilla Developer Network or W3Schools.

  3. #3
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,391

    Lightbulb

    Possible alternative...
    Code:
    <script type="text/javascript">
    String.prototype.toMMDDYY = function(sep) {  // expects input format of YYMMDD to convert to MM/DD/YY
      return this.substr(2,2)+sep+this.substr(4,2)+sep+this.substr(0,2)
    }
    
    // if short year IS KNOWN TO BE in this century
    String.prototype.toMMDDYYYY = function(sep) {  // expects input format of YYMMDD to convert to MM/DD/YYYY
      var tdate = this;
      return this.substr(2,2)+sep+this.substr(4,2)+sep+'20'+this.substr(0,2)
    }
    
    // whoops ... mis-read original request
    String.prototype.toDDMMYY = function(sep) {  // expects input format of YYMMDD to convert to DD/MM/YY
      return this.substr(4,2)+sep+this.substr(2,2)+sep+this.substr(0,2)
    }
    
    // test of above
    var str = '130125';  alert(str+' converts to '+str.toMMDDYY('/')+' or '+str.toMMDDYYYY('/')+' or '+str.toDDMMYY('/'));
        str = '121225';  alert(str+' converts to '+str.toMMDDYY('-')+' or '+str.toMMDDYYYY('-')+' or '+str.toDDMMYY('-'));
    </script>
    Last edited by JMRKER; 01-25-2013 at 09:39 PM.

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,268
    Or simply, with a regular expression :

    Code:
    var today="130126";
    var todayNewFormat=today.replace/(\d\d)(\d\d)(\d\d)/,'$3/$2/$1');
    alert(todayNewFormat);//=>26/01/13
    ;

  5. #5
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,391

    Thumbs up

    Quote Originally Posted by 007Julien View Post
    Or simply, with a regular expression :

    Code:
    var today="130126";
    var todayNewFormat=today.replace/(\d\d)(\d\d)(\d\d)/,'$3/$2/$1');
    alert(todayNewFormat);//=>26/01/13
    ;
    Nice, but I think you left out one '(' character ...
    Code:
    var today="130126";
    var todayNewFormat=today.replace(/(\d\d)(\d\d)(\d\d)/,'$3/$2/$1');
    alert(todayNewFormat);//=>26/01/13

  6. #6
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,268
    Sorry Srinuetta... Thanks JMRKER !
    We must write :
    Code:
    var todayNewFormat=today.replace(/(\d\d)(\d\d)(\d\d)/,'$3/$2/$1');

  7. #7
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    one simple way:
    Code:
       "yymmdd".split(/(.{2})/).reverse().join("")

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles