Getday()

M

mtek

Hi,

I'm kind of new to Javascript. I need to get the day or week from
the date entered by the user.

All the examples I see use Today.....but, I have the date in for
format: mm/dd/yyyy.

How can I use this function, with the parameter passed, to get the day
of the week???

Thanks!
John.
 
J

Joost Diepenmaat

Thanks for the reply. Not sure how that work in terms of receiving a
parameter (mm/dd/yyyy) and returning the day of week (0-6).

Just split the string up first, then pass the relevant numbers to
Date.UTC, and pass the result of that to Date():

var string = "01/03/2008";
var numbers = string.split('/');
var day = (new Date(Date.UTC(numbers[0] - 1900,numbers[1]-1,numbers[2]))).getUTCDay();
 
J

Joost Diepenmaat

Joost Diepenmaat said:
var day = (new Date(Date.UTC(numbers[0] - 1900,numbers[1]-1,numbers[2]))).getUTCDay();

erm.. that should probably be

var day = (new Date(Date.UTC(numbers[2] - 1900,numbers[1]-1,numbers[0]))).getUTCDay();
 
V

VK

Joost Diepenmaat said:
var day = (new Date(Date.UTC(numbers[0] - 1900,numbers[1]-1,numbers[2]))).getUTCDay();

erm.. that should probably be

var day = (new Date(Date.UTC(numbers[2] - 1900,numbers[1]-1,numbers[0]))).getUTCDay();

or even more easy:
var d = new Date('03/23/2008');
window.alert(d.getDay());
// ;-)

---------------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en-US">
<head>
<meta http-equiv="Content-type"
content="text/html; charset=iso-8859-1">
<title>Demo</title>
<script type="text/javascript">
var names = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday'
];

function getDayOfWeek(date) {
var d = new Date(date);
window.alert(names[d.getDay()]);
return false;
}
</script>
</head>
<body>
<form action="" onsubmit="return getDayOfWeek(this.date.value)">
<fieldset>
<legend>Demo</legend>
<input type="text" name="date" value="03/23/2008">
<input type="submit">
</fieldset>
</form>
</body>
</html>
 
R

RobG

Joost Diepenmaat said:
var day = (new Date(Date.UTC(numbers[0] - 1900,numbers[1]-1,numbers[2]))).getUTCDay();

erm.. that should probably be

var day = (new Date(Date.UTC(numbers[2] - 1900,numbers[1]-1,numbers[0]))).getUTCDay();


erm... the OP said the format was mm/dd/yyyy, the above seems to
expect dd/mm/y.
 
J

Joost Diepenmaat

RobG said:
Joost Diepenmaat said:
var day = (new Date(Date.UTC(numbers[0] - 1900,numbers[1]-1,numbers[2]))).getUTCDay();

erm.. that should probably be

var day = (new Date(Date.UTC(numbers[2] - 1900,numbers[1]-1,numbers[0]))).getUTCDay();


erm... the OP said the format was mm/dd/yyyy, the above seems to
expect dd/mm/y.

no, but Date.UTC expects the year *since 1900* as the first argument.
 
R

RobG

RobG said:
var day = (new Date(Date.UTC(numbers[0] - 1900,numbers[1]-1,numbers[2]))).getUTCDay();
erm.. that should probably be
var day = (new Date(Date.UTC(numbers[2] - 1900,numbers[1]-1,numbers[0]))).getUTCDay();
erm... the OP said the format was mm/dd/yyyy, the above seems to
expect dd/mm/y.

no, but Date.UTC expects the year *since 1900* as the first argument.

You split the provided string into an array, then assign the month
value to the date argument and the date value to the month argument.
 
J

Joost Diepenmaat

RobG said:
You split the provided string into an array, then assign the month
value to the date argument and the date value to the month argument.

Arh. You're right. American date formats keep confusing me. :)
 
D

Dr J R Stockton

Sat said:
I read that the date parser can be pretty finicky, so I would assume the
UTC() method would be more reliable if you already know the date format.

One can generally expect date software designs of US origin to accept or
presume FFF and to be compatible with those who can only read clocks
with hands.

Note :
K_ = 50000
D0_ = new Date()
Q_ = K_ ; while (Q_--) { }
D1_ = new Date()
Q_ = K_ ; while (Q_--) { new Date('03/23/2008') }
D2_ = new Date()
Q_ = K_ ; while (Q_--) { new Date('03/23/2008 GMT') }
D3_ = new Date()
Q_ = [D1_-D0_, D2_-D1_, D3_-D2_] // Demo 6

Times, P4/3G XP sp2
IE 7 16,797,594
Firefox 2.0.0.12 110,4172,453
Opera 9.26 32,953,234
Safari 3.1 15,235,93

As expected, GMT is faster.
 
D

Dr J R Stockton

Sun said:
no, but Date.UTC expects the year *since 1900* as the first argument.

Here, new Date(Date.UTC(2000, 0, 1)) gives a string form
representing 2000-01-01 00:00:00 in IE7, FF2, Op6, and Sf3. One does
get 1920 from new Date(Date.UTC(20, 0, 1)) however.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top