Skip to content
Advertisement

I am Having problems with showing dates without hours, minutes and seconds

I have an array of dates that I would like to put in a table (single column) and I am trying to show them without the hour minutes and seconds displaying. I am trying to use toDateString() but it returns it with an error (not a function). Is there a way to display just the dates out of this array;

let old_dates = [new Date(1990, 01, 01),new Date(1960, 01, 02),new Date(1996, 05, 22),new Date(1996, 05, 24),new Date(1996, 07, 24),new Date(1996, 10, 25),new Date(1996, 05, 17),new Date(1996, 02, 09),new Date(1996, 06, 30),new Date(1996, 07, 07),new Date(1996, 02, 14),new Date(1997, 11, 29),new Date(1997, 12, 17),new Date(1997, 10, 02),new Date(1997, 08, 06),new Date(1997, 02, 05),new Date(1998, 06, 15),new Date(1999, 10, 02),new Date(1999, 05, 21),new Date(1999, 02, 06),new Date(1999, 05, 22),new Date(2000, 01, 01),new Date(2000, 06, 09),new Date(2000, 09, 22),new Date(2000, 06, 12),new Date(2001, 04, 25),new Date(2001, 04, 11),new Date(2001, 05, 18),new Date(2002, 05, 07),new Date(2002, 03, 29),new Date(2002, 09, 16),new Date(1999, 05, 21),new Date(1999, 05, 21),new Date(1999, 05, 21),new Date(2002, 05, 21),new Date(2002, 05, 11),new Date(2002, 09, 14),new Date(2002, 04, 10),new Date(2002, 09, 10),new Date(2002, 04, 23),new Date(2002, 10, 09)];
console.log(old_dates);
let new_dates = old_dates.toDateString();
console.log(new_dates);

Advertisement

Answer

  • Tip: Don’t use leading zeroes in literal integers because JavaScript engines may interpret them as Base 8 (octal) instead of Base 10 (decimal).

    • To clarify: Older engines that predate ECMAScript 5 may interpret 012 (an octal number literal) as 10 in decimal.
    • ECMAScript 5+ requires numbers with leading-zeroes to be treated as octals only if all digits are in the range 0-7, so a literal 08 or 09 will still be interpreted as 8 and 9 in decimal.
    • When in ECMAScript 5+ 'use strict'-mode then the engine is required to always parse numbers with leading zeroes as decimals, not octals.
    • Though this does mean that if you have new Date( 1960, 012, 020 ) in a non-strict context then that will be interpreted as the 16th November 1960 (as Months are 0-based, not 1-based, and 12 in octal is 10 in Decimal, which is the 11th Month: November. Yes, this is confusing.).
  • Anyway, old_dates is an Array<Date>, not a Date.

    • So you’ll need to call toDateString() for each member of the array.

You can do that with a for loop, a for-of loop, or with Array.prototype.map, like so:

// Using loops:
for( let i = 0; i < old_dates.length; i++ ) {
    const dt = old_dates[i];
    console.log( dt.toDateString() )
}

for( const dt of old_dates ) {
    console.log( dt.toDateString() )
}

// Using Array.prototype.map
const formatted = old_dates.map( dt => dt.toDateString() ).join( ", " );
console.log( formatted );
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement