简体   繁体   中英

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:

{"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')
}

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