www.webdeveloper.com
Results 1 to 7 of 7

Thread: Convert date format

  1. #1
    Join Date
    May 2010
    Posts
    5

    Convert date format

    Hi,

    I modified a function I got from someone from this forum.

    It looks for date in the format "2011-09-15" and should convert it to "Sep 15 2011".

    It is not working. It is displaying the input itself. Can someone help.

    I really appreciate your time.

    Regards,
    Shiyam


    Code:
    function chgDteOnly(input)
    {
    
        // Input format is - 2011-09-15
        // Function output - Sep 15 2011                         
        
    	monthName = "Dmy,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec".split(/,/g);
    	var rgx=/(\d\d\d\d)-0?([\d]{1,2})-0?([\d]{1,2})\s0?/
    	return input.replace(rgx,function(all,y,m,d){
    			
    			return monthName[m]+" "+d+" "+y;
    		});
    }

  2. #2
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Passing the string directly to a date object should return an object with a time reference to which you can generate a date stamp like you want.

    Code:
    function getDateDate( inputStr ){
      timeRef = new Date( inputStr ).toGMTString().split(" ");
      return [timeRef[2], " ",timeRef[1],", ",timeRef[3]].join("");
    }
    alert( getDateDate("2011-09-15") );
    alert( getDateDate("2011/09/15") );
    for some reason, for which I am not going to waste my time pondering over it, the thing is fed the date for the 15th and out pops the 14th in the format as desired... Sept 14, 2011.

    Weird.

  3. #3
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Even if I change the way the input is treated...
    Code:
    function getDateDate( inputStr ){
      inputStr = inputStr.split("-");
      timeRef = new Date( inputStr[0] ,inputStr[1]-1, inputStr[2] ).toGMTString().split(" ");
      return [timeRef[2], " ",timeRef[1],", ",timeRef[3]].join("");
    }
    and you input a string of 2011-09-15 it will still output Sept 14, 2011 when the date function is passed the string in a format that it expects.

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    toGMTString() is deprecated. Use toUTCString() instead. See also:

    https://developer.mozilla.org/en/Jav...te/toGMTString

    But you should use neither of them in this case. Both toGMTString() and toUTCString() returned value may vary according to the platform, browser, browser version (at least so they say), thus it is not a reliable string to work with.

    On the other hand the most important thing is that IE7 and IE8 do not support the format (the hyphens): "YYYY-MM-DD" as a valid argument for the object/method Date(arguments).
    Last edited by Kor; 11-24-2011 at 06:25 AM.

  5. #5
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    So that I would have used other methods of the Date() object:
    Code:
    function getDateDate(inputStr){
    var d=new Date(inputStr.replace(/-/g,'/'));
    var m=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
    return m[d.getMonth()]+' '+d.getDate()+' '+d.getFullYear();
    }

  6. #6
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Quote Originally Posted by Kor View Post
    toGMTString() is deprecated. Use toUTCString() instead. See also:

    https://developer.mozilla.org/en/Jav...te/toGMTString

    But you should use neither of them in this case. Both toGMTString() and toUTCString() returned value may vary according to the platform, browser, browser version (at least so they say), thus it is not a reliable string to work with.

    On the other hand the most important thing is that IE7 and IE8 do not support the format (the hyphens): "YYYY-MM-DD" as a valid argument for the object/method Date(arguments).
    Its meant to be an RFC date format more often RFC-1123 or RFC-822 and therefore understood. Both UTC and GMT string will return the format thats identical.

    The toLocaleString() method
    Code:
    toLocaleString returns a string that is not year-2000 compliant
    and it is noted that it is not reliable.
    Be aware that this method, due to its nature, behaves differently depending on the operating system and on the user's settings.
    toGMTString may be depreciated but its still used even today, they shouldn't but they do and I do and its something I am used to typing more readily than UTC which will take some getting used to.

  7. #7
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Sir, if a company like Mozilla says that something is deprecated, that means W3C says the same. If so, it does not matter what you were used with. Just follow the standard.

Thread Information

Users Browsing this Thread

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

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