简体   繁体   中英

How to slice a timestamp and keeping it as integers in Google Scripts?

So I have a google sheet with Timestamps in the first column like:

row:1    12/19/2019 1:11:19
ro2:2    1/21/2020 0:16:13

When I use Logger.log(date); I get:

Thu Dec 19 2019 01:11:18 GMT-0800 (PST)

I am trying to get the month, day and year into separate variables, but keep them as integers. This is the only way I can slice the string, but I can't find ways to keep the day and month as integers.

var date = sheet.getRange(2,1).getValue();
var month = date.slice(0, 4);//I get the day as in Thur

I was wondering if there is a function to keep the day and month as numbers and not be converted to strings.

function dt() {
  var today=new Date();
  var year=today.getFullYear();//4 digit year
  var month=today.getMonth()+1;//January=1
  var day=today.getDate();//sunday=0 to  saturday=6
  var dts="1/20/2020";
  var slash1=dts.indexOf('/');
  var m=Number(dts.slice(0,slash1));
  var slash2=dts.indexOf('/',slash1+1);
  var d=Number(dts.slice(slash1+1,slash2));
  var y=Number(dts.slice(slash2+1));
  var end="isnear";//Run the debugger all the down to here and you can see all of the answers at one time.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM