Home > OS >  Google Apps Scripts Date conversions
Google Apps Scripts Date conversions

Time:10-12

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!

CodePudding user response:

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')
}
  • Related