0

Not sure what the terminology is so been unable to search the problem. I am wishing to extract the day from a datetime query (SQL), asp.net, c# etc and then display as in the following examples.

  • Date: 21/01/2019(UK) or 01/21/2019(US) display as 21st instead of 21
  • Date: 25/02/2018(UK) or 02/25/2018(US) display as 25th.

I have no problem extracting the day, I just need to know if it is possible to use the syntax as shown? Or what this syntax is even called?

Jim
  • 23
  • 3

1 Answers1

1

You can use momentjs format date.format('Do')

let dateStr = '21/01/2019';
let date = moment(dateStr, 'DD/MM/YYYY');
console.log(date.format('Do'));

dateStr = '02/25/2018';
date = moment(dateStr, 'MM/DD/YYYY');
console.log(date.format('Do'));

let dateStr = '21/01/2019';
let date = moment(dateStr, 'DD/MM/YYYY');
console.log(date.format('Do'));

dateStr = '02/25/2018';
date = moment(dateStr, 'MM/DD/YYYY');
console.log(date.format('Do'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62