kalyan
kalyan

Reputation: 375

How to Extract Month from a given date in MMM format in Google sheets using script

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.

Upvotes: 0

Views: 1605

Answers (2)

JSmith
JSmith

Reputation: 4808

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

Upvotes: 3

Cooper
Cooper

Reputation: 64032

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

Upvotes: 1

Related Questions