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

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

function showdate(){
var d=new Date(); 
alert(d)
}
Member Avatar for stbuchok

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>

Thanks it works fine

Is it possible to convert above codes into JQUERY?

Member Avatar for stbuchok

May I ask why?

Becuaes I think Jquery codes will be shorter.

Member Avatar for stbuchok

No, it won't.

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!
Member Avatar for stbuchok

How'd I miss that!? Yeah, this way is better.

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();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.