Closed Thread Icon

Preserved Topic: how to get the actual date on a string? (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=17886" title="Pages that link to Preserved Topic: how to get the actual date on a string? (Page 1 of 1)" rel="nofollow" >Preserved Topic: how to get the actual date on a string? <span class="small">(Page 1 of 1)</span>\

 
GRUMBLE
Paranoid (IV) Mad Scientist

From: Omicron Persei 8
Insane since: Oct 2000

posted posted 12-18-2000 02:03

what is the javascript function that gets the actual date and time?
i want to store that to a string and then write it on the document!
i think its something with getDate() ... but i dont know exactly!

thanx!

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 12-18-2000 02:48

Okay I just happened to have written this for a post in the gurusnetwork forum.

<script>

var DateVariable = new Date(); // Create a new date object

var seconds = DateVariable.getSeconds(); // This pulls out the seconds from your date object

var minutes = DateVariable.getMinutes(); // This pulls out the minutes from your date object


var hours = DateVariable.getHours(); // This pulls out the hours from your date object

if(hours >= 13) { hours = hours - 12; } // This just puts in in 12hr as opposed to a 24 hr clock

/**************************************************
This gets the month as a NUMBER JS starts with January as 0.
If you are using just a numerical date and you want it to make sense
you need to add the one to it

If you want to use text for the date replace the code with this:
monthNames = new Array('January', 'February','March','April','May','June','July','August','September'.'October','November','December');
var month = monthNames(DateVariable.getMonth());
********************************************************/

var month = DateVariable.getMonth() +1;

var day = DateVariable.getDate(); //Gets the Day of the Month
var year = DateVariable.getFullYear(); //Gets the full year 2000


</script>

I haven't tested this but it should all work.
I don't have my JS book with me otherwise I'd give you a little more.

The script I ripped this from is a cool one that actually displays the date as graphics. You can see it here http://www.bitdamaged.com/testpages/datetest.html



Walking the Earth like Kane

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 12-18-2000 03:15

Your not going to believe this, but I just finished writing my first script from scratch, and it was to print out the string "Today is Sunday December 17 2000". *edit* ( It prints out the current day and date)*/edit*

<script language= JavaScript>
function today(){
var today_is = new Date();
var month = today_is.getMonth();
var year = today_is.getFullYear();
var now = today_is.getDay();
var date = today_is.getDate();
cal = new Array();
cal[0] = 'January';
cal[1] = 'February';
cal[2] = 'March';
cal[3] = 'April';
cal[4] = 'May';
cal[5] = 'June';
cal[6] = 'July';
cal[7] = 'August';
cal[8] = 'September';
cal[9] = 'October';
cal[10] = 'November';
cal[11] = 'December';

for (i=0;i<month;i++);
if (i == month){
month = cal[i];
}

day= new Array();
day[0] = 'Sunday';
day[1] = 'Monday';
day[2] = 'Tuesday';
day[3] = 'Wednesday';
day[4] = 'Thursday';
day[5] = 'Friday';
day[6] = 'Saturday';

for (i=0;i<now;i++);
if (i == now){
alert('Today is ' + day[i] + ' ' + month + ' ' + date + ' ' + year);
}
}
//-->
</script>

Right now it just puts up an alert box with the date because I was just coming to the forum to ask how to print the string exactly where I want it on the web page. Your more than welcome to use the script if it was what you wanted. You will need to remove the alert() and change it to whatever you need. Now I'm going to make my own post to find out how to put it where I want it. Sorry my script isn't commented like Bitdamaged's, I had my hands full just writing it and getting it to work.

[This message has been edited by butcher (edited 18-12-2000).]

[This message has been edited by butcher (edited 18-12-2000).]

GRUMBLE
Paranoid (IV) Mad Scientist

From: Omicron Persei 8
Insane since: Oct 2000

posted posted 12-18-2000 10:55

thanx bitdamaged and butcher. both work great, BUT: (hehe)
it's actually not that what i wanted: im trying to do a guestbook in php and i want to add the date and time to every entry. is this possible just with JS/PHP or do i need more like a database ?
thx in advange...

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 12-18-2000 18:57

I haven't worked to much on dates in PHP. but what you guys both want to do is create a string variable with the date formatted the way you want it.

using butchers scipt you would do this
datestring = 'Today is ' + day[now] + ' ' + month + ' ' + date + ' ' + year;

Then in the page you would write.

<script>
document.write(datestring);
</script>

grumble you probably want to do this with straight PHP instead of using JS. what you want to do is include a hidden value in the form that delivers the date. I dont know the exact code but it should look something like this

<input type=hidden value= <php date function>>


Walking the Earth like Kane

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 12-18-2000 20:48

Hey bitdamaged -

I used your example above, and have two questions.

1. When I tried to swap out the code to use the text for the date, it didn't work. Cut and paste from your code....

2. If the seconds are between 0 and 10, it's displayed in a single digit. As in 12:30:4 instead of 12:30:04. Is there some syntax I can use to format in two digits?

Thanks!

Pat Richard
Web weenie
http://www.gurusnetwork.com
ICQ 23113317

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 12-18-2000 21:49

1. The correct code that will show month names instead of numbers is:

monthNames = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var month = monthNames[DateVariable.getMonth()];

2. To show time with leading zeros use something like:

if (hours < 10) { hours = '0'+hours; }
if (minutes < 10) { minutes = '0'+minutes; }
if (seconds < 10) { seconds = '0'+seconds; }



[This message has been edited by mr.maX (edited 18-12-2000).]

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 12-19-2000 01:00

You know, this JavaScript is almost starting to make sense to me now.......

Thanks for the help!

Pat Richard
Web weenie
http://www.gurusnetwork.com
ICQ 23113317

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 12-19-2000 02:23

Thanks Bitdamaged for answering my question, sorry for treading on your thread Grumble, but I gotta ask. Could either Bitdamaged or Mr. Max please explain to me why just because you define an array with the 'month' names in it, that var month = monthNames(DateVariable.getMonth()); returns the name of the month instead of the number? Please forgive my ignorance, I'm not doubting what you tell me, I just want to know why it works. Thanks again guys.

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 12-19-2000 08:16

When you define an array like this:

myArray = new Array('first','second','third','fourth','fifth');

It is the same as:

myArray = new Array();
myArray[0] = 'first';
myArray[1] = 'second';
myArray[2] = 'third';
myArray[3] = 'fourth';
myArray[4] = 'fifth';

You see, JavaScript will automatically assing indexes to the values in the first array, so you don't have to use myArray[index] = value...

Oh, and to get the value located at some specific index, you simply use:

var myValue = myArray[index]; // index: 0 = 'first', 1 = 'second', etc.

Notice the square brackets (bitdamaged made a syntax error there, because he used normal brackets).

So, when we apply all this to this script which shows date and time, this is how it works for months values. We first defined month names, JavaScript assigned indexes to their names (January = 0 ... December = 11), then we get the name of the current month by using var month = monthNames[DateVariable.getMonth()]; where DateVariable.getMonth() is the number of the current month, which is the same as defined indexes (if we put, say, zero instead of DateVariable.getMonth() inside square brackets, we'll get 'January' as result);

I hope that you'll understand now... <img border=0 align=absmiddle src="http://www.ozones.com/forum/biggrin.gif">



[This message has been edited by mr.maX (edited 19-12-2000).]

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 12-19-2000 21:33

Sure do, thanks Max.

GRUMBLE
Paranoid (IV) Mad Scientist

From: Omicron Persei 8
Insane since: Oct 2000

posted posted 12-20-2000 12:44

hey, thanx for your help guys...
i think i'll try the php method, which is then compaitble to all browsers

« BackwardsOnwards »

Show Forum Drop Down Menu