0

In a column, I have a date like 3/15/2020 20:41:29

Now how I can extract the month in MMM (like Jan, Feb, Mar etc) format.

The following I have tried but only get the month in integer and not in text form.

 var date = new Date("3/20/2020 20:41:29");
 var month = date.getMonth()+1;

but the output is 3 and not Mar (as in March).

How to get the month name like Mar, Apr, Jun etc.

kalyan
  • 121
  • 1
  • 3
  • 13

2 Answers2

3

using V8 one other solution would be to do:

const date = new Date("3/20/2020 20:41:29");
const month = new Intl.DateTimeFormat('en', { month: 'short' }).format(date)
Logger.log(month)

I highly recommend you have a look at this post

JSmith
  • 3,635
  • 3
  • 24
  • 36
1

Often you can use the Date() class constructor depending upon the string format.

function makeMyDate() {
  SpreadsheetApp.getUi().alert(Utilities.formatDate(new Date('3/15/2020 20:41:2'), Session.getScriptTimeZone(), "MMM"));
}

Utilities.formatDate()

Simple Date Format

Cooper
  • 36,005
  • 6
  • 17
  • 42