0

Hello Experts

I have following codes

<html>
<head>
<script language="Javascript">
function showdate()
{
var currentTime = new Date() 
var month = currentTime.getMonth() +1

var day = currentTime.getDate() 
var year = currentTime.getFullYear() 
var hours = currentTime.getHours() 
var minutes = currentTime.getMinutes() 
var seconds = currentTime.getSeconds() 
 
alert(month + "/" + day + "/" + year  +"\n" + hours+ ":"+ minutes+ ":"+ seconds) 
}
</script>
</head>
<body onload="showdate()">
</body>
</html>

the output is as

03/06/2012
14:41:46

But I want to get following result

Tuesday March 06, 2012 02:41:46 PM

Please modify my codes or suggest some others.

Thanks in advance

4
Contributors
9
Replies
13
Views
5 Years
Discussion Span
Last Post by Troy III
0

Hi, I think this should be better for showdate():

function showdate(){
var d=new Date(); 
alert(d)
}
1

Below will give you the exact output from your example:

<html>
<body>

<script type="text/javascript">

var DateTime = {
	getDate: function(){
		function addLeadingZero(data){
        		var returnValue = data.toString();
	
			if(data.toString().length === 1){
				returnValue = '0' + data;
			}
	
			return returnValue;
		}

		var d = new Date();

		var day = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
		var month = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

		var time = d.getHours() <= 12 ? addLeadingZero(d.getHours()) + ' ' + addLeadingZero(d.getMinutes()) + ' ' + addLeadingZero(d.getSeconds()) + ' AM' : addLeadingZero((d.getHours() - 12)) + ':' + addLeadingZero(d.getMinutes()) + ':' + addLeadingZero(d.getSeconds()) + ' PM';

		return day[d.getDay()] + ' ' + month[d.getMonth()] + ' ' + d.getDate() + ', ' + d.getFullYear() + ' ' + time;
	}
};


document.write(DateTime.getDate());

</script>

</body>
</html>
0

Thanks it works fine

Is it possible to convert above codes into JQUERY?

1
thisDay=
/*b.b. Troy III p.a.e*/{
    get Date(){return new Date().toLocaleDateString()},
    get Time(){return new Date().toLocaleTimeString()}
   }


thisDay.Date  >> "Thursday, March 08, 2012"
thisDay.Time  >> "09:49:38" or "9:49:38 AM" (IE) //will depend on a browser!

Edited by Troy III: n/a

0

Well...
It's Authorial, -that's most probably the sole reason! :')

it's shorter than any jQuery or whatever -But, it's a Gen5 and will not work on Gen4 browsers! :( so for backward compatibility use, one would need a step backward syntax:

thisDay=
/*b.b. Troy III p.a.e*/{
    Date: function(){return new Date().toLocaleDateString()},
    Time: function(){return new Date().toLocaleTimeString()}
   }

//and call:
 
    thisDay.Date();
    thisDay.Time();
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.