-1

I want to know how i can show or get only two numbers Date of current day date in Javascript,

So i can make same if else statements in javascript, for example like this in php

$dt = date("d");

 if ( $dt < 10) {then do this} or  if ( $dt > 20) {then do this.}

in php i get this 2 numbers current date with this simple command

echo date("d");

Anyone help me out how i can get this in javascript.

smallbee
  • 233
  • 1
  • 4
  • 16

1 Answers1

2

The simplest you could do is to use Date.prototype.getDate() :

const d = new Date().getDate();

if (d <= 10) { 
  console.log(`Date is: ${d}. Do this`);
} else if (d >= 20) {
  console.log(`Date is: ${d}. Do something else`);
}

Or you could opt for using date libraries like date-fns, moment.js or any other - for better timezone handling.

Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313