Category Archives: javascript snippets

Javascript: function showTime()

[javascript]
function showTime(){

myDate = new Date();

// Get time parts

hours = myDate.getHours();

minutes = myDate.getMinutes();

seconds = myDate.getSeconds();

// Just to display the time in a nice format

if (hours < 10) hours = "0" + hours;

if (minutes < 10) minutes = "0" + minutes;

if (seconds < 10) seconds = "0" + seconds;

actualTime = hours+":"+minutes+":"+seconds;

}
[/javascript]

Prompt Usage

Prompt usage snippet

			var track_number = prompt("Please enter tracking number:");
			if(track_number)
			{
				$(this).data('track_number', track_number);
				//$(this).closest('tr').children().eq(4).append('<div style="color:red; display:block;text-align: center;">[' + track_number + ']</div>');
			}
			else
			{ 
				/* do something else */
			}

Remove text from text input when get focus

<input type='text' style='margin:0 10px;background-color:#F3FAAA;width:250px;height:16px;vertical-align: text-bottom;;' name='search_text' id='search_field' value='Ex.: Dell Latitude D620 enter D620 only' onfocus='$(this).val(\"\").css(\"color\", \"#000000\")' 
	onblur='($(this).val() == \"\") ? $(this).val(\"Ex.: Dell Latitude D620 enter D620 only\").css(\"color\", \"#9B9B9B\") :  $(this)' />

Javascript function to get curent date

Here is the implementation of javascript function returning the current date in dd-mmm-yyyy format

[javascript]
function getCurDate(){
var months = new Array(“Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”, “Jul”, “Aug”, “Sep”, “Oct”, “Nov”, “Des”);
var myDate = new Date();

return myDate.getDate() + ‘-’+ months[myDate.getMonth()] + ‘-’ + myDate.getFullYear() ;
}
[/javascript]
Hope it helps!