1

can someone explain me what is Javascript doing there?

Why I have to divide the ms through 1 to convert the date?

d1=new Date(board.resultsbox.value/1);
d2=new Date(board.resultsbox.value);
board.resultsbox1.value=d1; //Fri Nov 01 2013 00:30:00 GMT+0100 (CET)
board.resultsbox2.value=d2; //Invalid Date

Thanks and have a nice weekend.

<html>
<head>
  <title>calculator based on javascript.</title>
</head>
<body>
  <form name="board" id="board">
    <input type="input" id="resultsbox" value="1383262200000">
    <input type="input" id="resultsbox1">
    <input type="input" id="resultsbox2">
  </form>
  <input type="button" onclick="
    d1=new Date(board.resultsbox.value/1);
    d2=new Date(board.resultsbox.value);
    board.resultsbox1.value=d1;
    board.resultsbox2.value=d2;" style="height: 80px;" value="=">
</body>
</html>
pats
  • 133
  • 1
  • 1
  • 8
  • When dividing a string `board.resultsbox.value` by one, JS attempts to convert string to a number. `Date` expects a number. –  May 25 '18 at 14:25

5 Answers5

1

Because board.resultsbox.value is a String value. When you add mathematical operation (in this case dividing by 1), it is automatically converted to a numeric value which is acceptable by Date constructor. Same (but cleaner) could be achieved using parseInt function like:

var millis = parseInt(board.resultsbox.value);

You can read more about Date constructor here.

Also, there are plenty of information about how to convert String to number, like for example this Stack Overflow thread.

Vaidas
  • 1,494
  • 14
  • 21
1

Unlike what one may think, the casting is not the issue here but the format is.

board.resultsbox.value is a String

This is acceptable because new Date() has the following valid sets of arguments:

new Date();
new Date(value);
new Date(dateString);
new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);

But evidently, board.resultsbox.value is a String representing a numerical timestamp. new Date(dateString) fails because it accepts Strings parsable by Date.parse() (example '01 Jan 1970 00:00:00 GMT') while needs numerical timestamps to be passed as Number.

The division is one implicit way to cast the String to Number. Therefore the correct format and argument set are met and new Date() works.

Attersson
  • 4,755
  • 1
  • 15
  • 29
0

This divide by one is one of the ways (actually strange one) to convert string into number. I assume that your value is coming as a string, and to be parsed as a date seed it needs to be a number

0

The value returned by a textfield is a string, the constructor where you can pass a value to new Date expects a number.

There are a few of ways to convert a string to a number in javascipt, some of which are:

  • parseInt
  • +"1234"
  • "1234"/1

It seems the author of this code went for the latter example

Jamiec
  • 133,658
  • 13
  • 134
  • 193
0

You need to parse it to number then convert it to date:

<html>
<head>
  <title>calculator based on javascript.</title>
</head>
<body>
  <form name="board" id="board">
    <input type="input" id="resultsbox" value="1383262200000">
    <input type="input" id="resultsbox1">
    <input type="input" id="resultsbox2">
  </form>
  <input type="button" onclick="
    d1=new Date(board.resultsbox.value/1);
    d2=new Date(parseFloat(board.resultsbox.value));
    board.resultsbox1.value=d1;
    board.resultsbox2.value=d2;" style="height: 80px;" value="=">
</body>
</html>
mbadeveloper
  • 1,272
  • 9
  • 16