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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    430
    Thanks
    6
    Thanked 3 Times in 3 Posts

    how to get local time in dd-mm-yyyy format ?

    How would I get the local time of a pc using JavaScript in dd-mm-yyyy format ?

  • #2
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Date in dd/mm/yyyy format

    Code:
    var wholeDate = getTheDay();
    
    
    function getTheDay(){
    
    	var d = new Date();
    	var numMonth = d.getMonth() + 1;
    	var day = d.getDate();
    	var year = d.getFullYear();
    	var whole = numMonth + "/" + day + "/" + year;
    	
    	return whole;
    
    }
    getTheDay() will return dd/mm/yyyy format

    First it makes a new date, where it will then return the Month number (0-11). You have to add one because there are 12 months and all that JS junk. To get the day number you use getDate();. For the year, you use getFullYear;

    Obviously these all go into variables. Then just add those variables into a concatenated string. Then you have to return the string.

    And since you can't access JavaScript functions without some sort of onclick method, you do this in your html:

    Code:
    <p>
    <script type="jsfilehere.js">document.write(wholeDate);</script>
    </p>

  • #3
    Regular Coder
    Join Date
    Oct 2009
    Posts
    430
    Thanks
    6
    Thanked 3 Times in 3 Posts
    thank you, one last thing, how would i switch the date that was entered, i.e.

    dd-mm-yyyy to yyyy-mm-dd

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Quote Originally Posted by needsomehelp View Post
    thank you, one last thing, how would i switch the date that was entered, i.e.

    dd-mm-yyyy to yyyy-mm-dd
    Change this line
    var whole = numMonth + "/" + day + "/" + year;

    to
    var whole = year + "/" + numMonth + "/" + day;

    Change / to - if required.
    Last edited by Philip M; 12-18-2011 at 01:43 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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