简体   繁体   English

Google Apps 脚本日期转换

[英]Google Apps Scripts Date conversions

I am populating a Google sheet with data from a JSON object I receive from Postman. Some of the information I receive is in this format:我正在使用来自 JSON object 的数据填充 Google 表格,我从 Postman 收到。我收到的一些信息采用这种格式:

{"attempt_starttime": "2021-09-21T03:28:13+0000",
 "attempt_endtime":"2021-09-21T03:28:35+0000",
 "invited_on":"2021-09-21T03:27:50+0000"}

I need to do some manipulations on them.我需要对它们进行一些操作。 What I want to know is我想知道的是

  1. How do I use an apps script to convert the data into a day of the week(Monday, Tuesday, etc.)如何使用应用程序脚本将数据转换为星期几(星期一、星期二等)
  2. How can I use apps script to do subtraction on the dates so I can get data like how long did it take the person to complete the task, how many days did it take to start the task after being invited, etc.我如何使用应用程序脚本对日期进行减法,以便我可以获得数据,例如此人完成任务需要多长时间,被邀请后开始任务需要多少天等。

Thank you!谢谢!

Adapt as you need...根据需要调整...

function chrono() {
  var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
  var jsonString = `{"attempt_starttime": "2021-09-21T03:28:13+0000",
 "attempt_endtime":"2021-09-21T03:28:35+0000",
 "invited_on":"2021-09-21T03:27:50+0000"}`
  var data = JSON.parse(jsonString)
  var startTime = new Date(data.attempt_starttime)
  var endTime = new Date(data.attempt_endtime)
  var invitedTime = new Date(data.invited_on)
  console.log(startTime + ' ... ' + days[startTime.getDay()])
  console.log(endTime + ' ... ' + days[endTime.getDay()])
  console.log(invitedTime + ' ... ' + days[invitedTime.getDay()])

  console.log(invitedTime.getUTCHours()); 
  console.log(invitedTime.getUTCMinutes());
  console.log(invitedTime.getUTCSeconds());

  console.log((startTime-invitedTime)/1000 + ' secondes')
  console.log((endTime-startTime)/1000 + ' secondes')
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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