...

View Full Version : Displaying a Date with another format



brock029
09-06-2011, 02:02 AM
Alright so I've been searching around trying to figure out how to change the date format, the way I have seen it the most is like


var d=new Date();
var datestring=d.getdate + d.getMonth + d.get Year

I am new to javascript and this is the first script I have written on my own so this is what it does. It is supposed to display the date that a shipment should arrive on with 2 day shipping through ups so it skips weekends. It displays the date after either adding 2 days or more depending on the day of the week. But the way the date is formatted really needs to be changed. So if any one can suggest maybe how to tie in the above code with mine that would be great.


<script type="text/javascript">
var d=new Date();
var day=d.getDay();
if (day <= 3){
d.setDate (d.getDate() + 2);
}
else if (day == 4) {
d.setDate (d.getDate() + 4)
}
else if (day == 5){
d.setDate (d.getDate() + 4);
}
else if (day == 6) {
d.setDate (d.getDate() + 3);
}
document.write("Your card should arrive on " + d);
</script>

DaveyErwin
09-06-2011, 02:39 AM
var d=new Date();
var datestring=d.getDate() +" "+ d.getMonth() +" "+ d.getYear()
alert(datestring)


Don't forget about Holidays:)

brock029
09-06-2011, 03:01 AM
Yeah Holidays is the next step but first I have to figure out how to lol. So my next question is how do i tie

var datestring=d.getDate() +" "+ d.getMonth() +" "+ d.getYear()
alert(datestring)


into


<script type="text/javascript">
var d=new Date();
var day=d.getDay();
if (day <= 3){
d.setDate (d.getDate() + 2);
}
else if (day == 4) {
d.setDate (d.getDate() + 4)
}
else if (day == 5){
d.setDate (d.getDate() + 4);
}
else if (day == 6) {
d.setDate (d.getDate() + 3);
}
document.write("Your card should arrive on " + d);
</script>



To make it show the correct date that its adding on days for....for example if you load the scirpt up right now it shows "Your card should arrive on Wed Sep 7 19:14:46 CDT 2011" and if i just add in the var datestring it gives me "Your card should arrive on 5 8 2011 " which is the wrong month and day. I think I messed up somewhere.

EDIT:
Its getting the correct day just the wrong month.

brock029
09-06-2011, 03:10 AM
Ok so I figured out one part of my problem get month works from 0-11 not 1-12 so is there a way to change that?

brock029
09-06-2011, 03:20 AM
Ok fixed it and got it working pretty good it now displays "Your card should arrive on Wednesday,September 7,2011"


<script type="text/javascript">
var d=new Date();

//var for counting days
var day=d.getDay();

//array for naming the days
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";

//array for naming the months
var month=new Array(12);
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";

//statements to do the math of adding two business days
if (day <= 3){
d.setDate (d.getDate() + 2);
}
else if (day == 4) {
d.setDate (d.getDate() + 4)
}
else if (day == 5){
d.setDate (d.getDate() + 4);
}
else if (day == 6) {
d.setDate (d.getDate() + 3);
}

//var to display the date better
var datestring=month[d.getMonth()] +" "+d.getDate() +","+ d.getYear();

//output to display the date
document.write("<b><i>Your card <font color=red>should</font> arrive on</i></b> " + weekday[d.getDay()] +","+ datestring);
</script>

jmrker
09-06-2011, 03:48 AM
If I'm understanding what you want to do,
you could simplify it like this... :eek:


<!DOCTYPE HTML>
<html>
<head>
<title> Untitled </title>
<script type="text/javascript">
// From:

var d=new Date();
var day=d.getDay();
var weekday=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];

var month=['January','February','March','April','May','June',
'July','August','September','October','November','December'];
var d2a = 0;
switch(day) {
case 4: d2a = 4; break; // Thursday
case 5: d2a = 4; break; // Friday
case 6: d2a = 3; break; // Saturday
default: d2a = 2; break; // all other days
}
d.setDate(d.getDate()+d2a);

var datestring=month[d.getMonth()] +" "+d.getDate() +","+ d.getFullYear();
var str = "<b><i>Your card <font color=red>should</font> arrive on</i></b> "
+ weekday[d.getDay()] +", "+ datestring;
document.write(str);
</script>

</head>
<body>

</body>
</html>

brock029
09-06-2011, 04:10 AM
Thats exactly it, does the same thing I wrote just alot cleaner...now to add holidays in the mix and it would be perfect! But one thing what exactly does the


switch(day) {
case 4: d2a = 4; break; // Thursday
case 5: d2a = 4; break; // Friday
case 6: d2a = 3; break; // Saturday
default: d2a = 2; break; // all other days
}


part of it do....or how does it work?

jmrker
09-06-2011, 04:58 AM
The "switch" checks the value of 'day', which ranges from 0...6
In the case the day is 4, it assigns 4 to the variable 'd2a'
If the day is 0, 1, 2 or 3, the default assignment is 2.

The assignment is added to the datestring display you already had coded.

See here (http://www.w3schools.com/js/js_switch.asp) for more explanations about the 'switch' statement.
or do a 'google' search on 'javascript switch' for other sites.

jmrker
09-06-2011, 05:10 AM
You can check for holidays with javascript using one of the following functions:
See: http://www.softcomplex.com/forum/viewthread_2814/
or: http://www.hotscripts.com/listing/federal-holiday/

Probably the easiest way would be to form the expected delivery date as you already do
and then check it against a list of holiday dates set-up in an array for the year's dates
If a match is found then add in an extra day (??? or more) before you make the final display.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum