0

I have to do an exercise on dates and times in Javascript. This is the text of the exercise:

Write instructions that allow you to view (with the document.write () method) the current date and time in this way: Saturday 30 April 2011, 12:05

I declared two variables, months and days. Then I created two arrays, one containing the months and the other containing the days of the week. Then I tried to extract the day of the week and the month from the arrays.

var date = new Date(); 
var set, gg, mm, aaaa, h, m; 

var month = new Array(12); 
month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

var days = new Array(7); 
days = ["Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun"]; 

set = days[date.getDay()] + " "; 
gg = date.setDate(6) + " "; 
mm = month[date.setMonth(3)] + " "; 
aaaa = date.setYear(2011); 
h = date.setHours(12) + ":"; 
m = date.setMinutes(05);

document.write(set + gg + mm + aaaa + ", " + h + m);

I expect the output of this code to be "Saturday 30 April 2011, 12:05", but the actual output is "Sat 1565123795470 undefined 1302122195470, 1302086195470:1302084335470".

E_net4
  • 27,810
  • 13
  • 101
  • 139
  • these setter in Date object returns the [number of milliseconds between 1 January 1970 00:00:00 UTC and the updated date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setMinutes#Return_value). – guijob Aug 16 '19 at 21:09
  • You might want to see [*How to format a JavaScript date*](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date). – RobG Aug 17 '19 at 04:31

3 Answers3

1

Just as an alternative, you might use:

const fullDateOptions = {
   weekday: 'long',
   year: 'numeric',
   month: 'long',
   day: 'numeric',
   hour: '2-digit',
   minute:'2-digit',
   hour12: false,
};

new Date().toLocaleString(
   'en-AU',
   fullDateOptions
);

IMHO: It's more scalable than hard-coding dates though might have some browser-dependent implementation particularities.

Also check compatibility section for additional information.

Igor Bykov
  • 2,532
  • 3
  • 11
  • 19
0

You are using sets rather than gets, setting a date/year etc will return the timestamp which is why your string is being built of timestamps.

If you switch the .setDate(6) for example to getDate(), then it will correctly get the day of the month

var date = new Date(); 
var set, gg, mm, aaaa, h, m; 

var month = new Array(12); 
month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

var days = new Array(7); 
days = ["Mon", "Tue", "Wed", "Tue", "Fri", "Sat", "Sun"]; 

set = days[date.getDay()] + " "; 
gg = date.getDate() + " "; 
mm = month[date.getMonth()] + " "; 
aaaa = date.getFullYear(); 
h = date.getHours() + ":"; 
m = date.getMinutes();

document.write(set + gg + mm + aaaa + ", " + h + m);

I've also switched to use getFullYear as getYear is not recommended.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getYear

AHB
  • 548
  • 2
  • 7
-1

I see you used set instead of get. This probably caused a lot of confusion.

Another thing I noticed is that you started your week with Monday. this might seem natural, but in this case (and in most software implementations), it should start with Sunday. It is also advised you use getFullYear as getYear is deprecated. Here is my try at the code:

var date = new Date(); 
var set, gg, mm, aaaa, h, m; 

var month = new Array(); 
month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

var days = new Array(); 
days = ["Sun", "Mon", "Tue", "Wed", "Tue", "Fri", "Sat"]; 

set = days[date.getDay()] + " "; 
gg = date.getDate(6) + " "; 
mm = month[date.getMonth(3)] + " "; 
aaaa = date.getFullYear(2011); 
h = date.getHours(12) + ":"; 
m = date.getMinutes(05);

document.write(set + gg + mm + aaaa + ", " + h + m);

This gets me the wanted result.

To be honest, I think a bit of internet searching would have gotten you there as well. here is the link I found: https://javascript.info/date

thisisbenmanley
  • 386
  • 2
  • 9
Kasper
  • 42
  • 1
  • 4