Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Omitting Seconds in Date() 1

Status
Not open for further replies.

Tivoli0

MIS
Dec 7, 2001
41
IL
Hi all,

I have a
Code:
var dateField = new Date();
that produce a date and time with seconds. How can I omit the seconds so it'll show:

Sat May 31 6:55 AM

Thanks! -Tivoli0
 
The following code should do it for you.

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>

var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var timeValue = &quot;&quot; + ((hours >12) ? hours -12 :hours)
timeValue += ((minutes < 10) ? &quot;:0&quot; : &quot;:&quot;) + minutes
timeValue += (hours >= 12) ? &quot; P.M.&quot; : &quot; A.M.&quot;
timerRunning = true;


mydate = new Date();
myday = mydate.getDay();
mymonth = mydate.getMonth();
myweekday= mydate.getDate();
weekday= myweekday;
myyear= mydate.getYear();
year = myyear

if(myday == 0)
day = &quot; Sunday, &quot;

else if(myday == 1)
day = &quot; Monday, &quot;

else if(myday == 2)
day = &quot; Tuesday, &quot;

else if(myday == 3)
day = &quot; Wednesday, &quot;

else if(myday == 4)
day = &quot; Thursday, &quot;

else if(myday == 5)
day = &quot; Friday, &quot;

else if(myday == 6)
day = &quot; Saturday, &quot;

if(mymonth == 0)
month = &quot;January &quot;

else if(mymonth ==1)
month = &quot;February &quot;

else if(mymonth ==2)
month = &quot;March &quot;

else if(mymonth ==3)
month = &quot;April &quot;

else if(mymonth ==4)
month = &quot;May &quot;

else if(mymonth ==5)
month = &quot;June &quot;

else if(mymonth ==6)
month = &quot;July &quot;

else if(mymonth ==7)
month = &quot;August &quot;

else if(mymonth ==8)
month = &quot;September &quot;

else if(mymonth ==9)
month = &quot;October &quot;

else if(mymonth ==10)
month = &quot;November &quot;

else if(mymonth ==11)
month = &quot;December &quot;

document.write(day + month + myweekday + &quot; &quot; + timeValue);

</SCRIPT>
 
Hey guys,

With dates I found that we could really simplify the code if we create a library to do all the work for us. Using prototype and arrays you can really make things go great for you!

// sample library code
Date.prototype.weekDayNames = [&quot;Sunday&quot;,&quot;Monday&quot;, &quot;Tuesday&quot;,&quot;Wednesday&quot;,&quot;Thursday&quot;,&quot;Friday&quot;,&quot;Saturday&quot;]

Date.prototype.getWeekDayName = function()
{
return this.weekDayNames[this.getDay()]
}

// sample code
var today = new Date()
document.write(&quot;Today is &quot; + today.getWeekDayName())

Let me know what you guys think. Prototyping really brings your code to a new level and makes it much more readable too.

Gary Haran
==========================
 
Hi xutopia,

Sound good to me.

I agree with your approach and will check it out.
Thanks for your insight and comment! -Tivoli0
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top