...

View Full Version : how to get local time in dd-mm-yyyy format ?



needsomehelp
12-18-2011, 01:30 AM
How would I get the local time of a pc using JavaScript in dd-mm-yyyy format ?

Fluttershy
12-18-2011, 01:57 AM
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:


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

needsomehelp
12-18-2011, 02:07 PM
thank you, one last thing, how would i switch the date that was entered, i.e.

dd-mm-yyyy to yyyy-mm-dd

Philip M
12-18-2011, 02:41 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum