google-apps-scriptgoogle-workspace-add-ons

Google App Script: DateTimePicker object shows time in UTC time zone


In our Gmail add-in we are using DateTimePicker class. Due to documentation for setTimeZoneOffsetInMins function, if we aren't setting time zone, datepicker should show time in user's time zone. However, when we are inserting initial date into widget widget.setValueInMsSinceEpoch(Date.now()) it shows UTC time instead of local time. Checked in google calendar - time zone set is not UTC. Maybe we are missing something here?


Solution

  • Date Time Picker shows Local Time

    To get the output, you just need the method getTimeZoneOffset() to get the user timezone and add the Class DateTimePicker Method setTimeZoneOffsetInMins().


    Here's the Code:

    function createHomepageCard() {
    
      let userTimeZone = new Date().getTimezoneOffset()*-1
    
      let cardSection1DatePTimePicker1 = CardService.newDateTimePicker()
        .setFieldName('dateTime')
        .setTitle('Pick a date and time')
        .setValueInMsSinceEpoch(Date.now())
         // Set your Value here.
        .setTimeZoneOffsetInMins(userTimeZone)
    
      let cardSection1 = CardService.newCardSection()
        .addWidget(cardSection1DatePTimePicker1);
    
    
      let card = CardService.newCardBuilder()
        .addSection(cardSection1)
        .build();
      return card;
    }
    

    Here is the Sample Output:

    Sample Add-on Output


    Reference: