Home > Enterprise >  Javascript How to get today and tomorrow date format like "22.11.2022 Tuesday"
Javascript How to get today and tomorrow date format like "22.11.2022 Tuesday"

Time:11-22

I couldn't find the solution. How to get date in "Tuesday 22.11.2022" format.

this is how i did it

 const date = new Date();
  const day = date.getDate();
  const month = date.getMonth()   1;
  const year = date.getFullYear();
  const today = day   '.'   month   '.'   year;
  const tomorrow = day   1   '.'   month   '.'   year;

Is there any way to get today's and tomorrow's date?

CodePudding user response:

Use the built-in Intl.DateTimeFormat. You can either replace / with . in the date or use a locale that prefers dots (Turkish for example).

const today = new Date();
const tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate()   1);

const locale = "tr"; // set to `undefined` to use the browser default

// Using "en" for the day name since you seem to want it in English
const dayFormatter = new Intl.DateTimeFormat("en", { weekday: "long" });
const dateFormatter = new Intl.DateTimeFormat(locale, {
  year: "numeric",
  month: "2-digit",
  day: "2-digit",
});

const formatDate = (date) =>
  `${dayFormatter.format(date)} ${dateFormatter.format(date)}`;

console.log("today:", formatDate(today));
console.log("tomorrow:", formatDate(tomorrow));

Note: JavaScript's Date does not deal with September 1752

CodePudding user response:

Updated

With Vanilla JS

const options = {
    weekday: 'long',
    day: 'numeric',
    month: 'numeric',
    year: 'numeric',
};

const date = new Date();
var date2 = new Date();
date2.setDate(date.getDate()   1);


const today = date.toLocaleDateString(
    'en-gb', options
).split('/').join('.').replace(/,/g, '');

const tommorow = date2.toLocaleDateString(
    'en-gb', options
).split('/').join('.').replace(/,/g, '');


console.log(today);
console.log(tommorow);

With MomentJs

var today = moment().format('dddd DD.MM.YYYY');
var tomorrow =  moment().add(1, 'days').format('dddd DD.MM.YYYY');

console.log(today);
console.log(tomorrow);
<script src="https://momentjs.com/downloads/moment.min.js"></script>

CodePudding user response:

dateFns.format(new Date, "DD.MM.YYYY dddd")

I think it's very hard to do with vanilla js, because assume today is march 31, you get the day equal to 31, you get tomorrow by plus 1 to it => and you got 32

  • Related