0

I have a date:

 console.log(lastDayDate)

Output:

Sun Apr 30 2023 00:00:00 GMT-0500 (Central Daylight Time)

Then I use:

formattedDate = lastDayDate.toLocaleString().split(',')[0];
console.log(formattedDate)

Output is:

 4/30/2023

How can I change it to :

04/30/2023
William
  • 3,724
  • 9
  • 43
  • 76
  • Does this answer your question? [How can I pad a value with leading zeros?](https://stackoverflow.com/questions/1267283/how-can-i-pad-a-value-with-leading-zeros) – Felix Apr 28 '23 at 03:12

5 Answers5

3

You can use Date#toLocaleDateString with additional options.

console.log(new Date().toLocaleDateString('en-US', {
  day: '2-digit',
  month: '2-digit',
  year: 'numeric'
}));
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
0

You could fix it with:

let lastDayDate = new Date("Sun Apr 30 2023 00:00:00 GMT-0500 (Central Daylight Time)");

let day = String(lastDayDate.getDate()).padStart(2, "0");
let month = String(lastDayDate.getMonth() + 1).padStart(2, "0"); // Se suma 1 porque getMonth() devuelve el mes de 0 (enero) a 11 (diciembre)
let year = lastDayDate.getFullYear();

let formattedDate = `${month}/${day}/${year}`;

console.log(formattedDate); // Debería imprimir: 04/30/2023`
Wongjn
  • 8,544
  • 2
  • 8
  • 24
  • You should keep your answers in english. And explain a bit why your solution works for OP's problem. As it is it's more of a code only answer which is discouraged. – kevinSpaceyIsKeyserSöze Apr 28 '23 at 06:52
0

You can split the 4/30/2023 bit with / and check whether the date/month is less than 10. Created a function for that (also checks the date):

let formattedDate = "4/30/2023";

function addZero(originalDate){
  date = originalDate.split("/");
  for (let i = 0; i < 2; i++){
    if (+date[i] < 10){ // Shorthand of parseInt
      date[i] = "0" + date[i];
    }
  }
  return date.join("/");
}
console.log(addZero(formattedDate));

:)

0

Since you are using JavaScript, there are a lot of awesome 3'rd party libraries out there that can help you play around date object. The most advisable 2 are momentjs and date-fns.

Creating a moment instance of a js Date() object is quite easy,

import moment from 'moment';
const timestamp = new Date();
const momentTimestamp = moment(timestamp);

momentTimestamp.format('MM/DD/YYYY')

moment.js also has a crazy amount of formats available, for all other requirements.

JJY9
  • 100
  • 9
  • [Moment.js is dead](https://momentjs.com/docs/) – kevinSpaceyIsKeyserSöze Apr 28 '23 at 06:49
  • Thanks @kevinSpaceyIsKeyserSöze for the intel, i actually started using moment.js for the timezone functionality that it provides. It helps to assign a specific timezone and after that all dates will reflect that timezone. – JJY9 Apr 29 '23 at 11:58
0

you can try with moment. it's very simple to use

If you are not using a package manager, you can just include the script directly in HTML:

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.26.0/moment.min.js" integrity="sha256-5oApc/wMda1ntIEK4qoWJ4YItnV4fBHMwywunj8gPqc=" crossorigin="anonymous"></script>

Then, it would be available to import like this:

const moment = require('momentjs');
// or
import moment from 'momentjs';

const now = moment().format('MM/DD/YYYY');
console.log(now); // "05/12/2023"
Siddhartha Mukherjee
  • 2,703
  • 2
  • 24
  • 29