Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    184
    Thanks
    140
    Thanked 0 Times in 0 Posts

    Adding functionality for a date format of mmm dd, yyyy into existing function

    Hello,

    I really need your help,

    How can I add existing functionality and build upon my existing function to also generate a dateformat of mmm dd, yyyy (ie. Feb 10, 2014)

    So lets say for example: gatedate('testdate') generates: Feb 10, 2014

    Code:
    function getdate(type) {
    
        var today = new Date()
        var time = today.toLocaleTimeString().replace(/([\d]+:[\d]{2})(:[\d]{2})(.*)/, "$1$3")
    
        var dd = today.getDate()
        var mm = today.getMonth()+1;
        var yyyy = today.getFullYear();
    
        if(dd<10){ dd='0'+dd }
        if(mm<10){ mm='0'+mm }
    
        today = dd+'/'+mm+'/'+yyyy
    
        if (type === 'system') {
    
            return today +" "+time
        }
    
        else if (type === 'testdate') {
    
            return Feb 10, 2014
        }
    
        else {
            return today
        }
    
    }//end function

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,083
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Question

    I don't really understand what it is that you are trying to do with your code
    so this is just a guess with some modified code.

    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> HTML5 page </title>
    
    </head>
    <body>
    <script type="text/javascript">
    function getdate(dt) {
      var today = new Date();
      var time = today.toLocaleTimeString().replace(/([\d]+:[\d]{2})(:[\d]{2})(.*)/, "$1$3");
    
      if (dt === 'system') { return today.toDateString() +" "+time; }
      if (dt === 'testdate') { return 'testdate hard-coded as: Feb 10, 2014'; }
    
      var dd = today.getDate();
      var mm = today.getMonth()+1;
      var yyyy = today.getFullYear();
    
      if(dd<10){ dd='0'+dd; }
      if(mm<10){ mm='0'+mm; }
    
      today = dd+'/'+mm+'/'+yyyy;
      return today; 
    
    } //end function
    
    alert(getdate('Feb 10, 2014'));  // return current date in dd/mm/yyyy format
    alert(getdate('system')); // returns system date modified with 'time' regular expression modification
    alert(getdate('testdate')); // return hard-coded date of Feb 10, 2014
    alert(getdate('Feb 14, 2014'));  // return current date in dd/mm/yyyy format
    </script>
    
    </body>
    </html>

  • Users who have thanked jmrker for this post:

    jason_kelly (02-13-2014)

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    184
    Thanks
    140
    Thanked 0 Times in 0 Posts
    Thanks, but I more thinking along the lines of:

    Code:
    function getdate(type) {
    
    	var today = new Date()
    
    	var time = today.toLocaleTimeString().replace(/([\d]+:[\d]{2})(:[\d]{2})(.*)/, "$1$3")
    	var monthNames = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
    
    	var d = today		
    	var dd = today.getDate()
    	var mm = today.getMonth()+1;
    	var yyyy = today.getFullYear();
    		
    	if(dd<10){ dd='0'+dd }
    	if(mm<10){ mm='0'+mm }
    		
    	today = dd+'/'+mm+'/'+yyyy
    
    	if (type === 'system') {
    	
    		return today +" "+time
    	}
    	
    	else if (type === 'shortdate') {	
    		return monthNames[d.getMonth()] + ' ' + dd + ', ' + yyyy
    	}
    	
    	else {
    		return today
    	}
    
    }//end function

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,083
    Thanks
    38
    Thanked 498 Times in 492 Posts
    As I said, I was not sure of your intended purpose.

    If you are satisfied with your solution, mark thread as resolved.
    If not, please state what it is that you are trying to do.

    Either way, good luck!

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,335
    Thanks
    11
    Thanked 588 Times in 569 Posts
    if you convert the standardized UTC string into local time, you can easily and predictably extract it's parts to build custom formats in the local language:

    Code:
    var parts=new Date( new Date() - (new Date().getTimezoneOffset() * 60000)).toUTCString().split(/\W+/);
    
    alert(parts[2]+" " + parts[1]+", "+parts[3]) // shows "Feb 12, 2014"
    for your use, swap the red with "today", without quotes.
    i find that chopping known dates is simpler than hand-building using long-winded methods like date.getMinutes()...
    Last edited by rnd me; 02-13-2014 at 02:46 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

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