var pickupTimeOld = wysylka.getRange("C5").getValue()
var pickupTime = Utilities.formatDate(pickupTimeOld, "GMT+1", 'HH:mm')
I am facing an issue while taking the value from google sheets which is for example 9:50:00, it's changing the time to 09:26. I can not find where it subtracts 24 minutes, as from the code everything is fine. Is ther eany possibilty that the confiuguration of google sheets make that issue? What can be reason of this substract.
Screen of formating in google sheets:
Chances are that the issue is caused by your code not observing the date component of the datetime value you want to present as HH:mm.
Your code gets a datetime value from the spreadsheet, but based on the screenshot, it looks like that datetime value only has a time component. That means that the date component will default to the spreadsheet epoch, i.e., 30 December 1899. Timezone offsets used to be pretty weird in 1899, so if you omit the date component, you may get surprising results.
Even today, not all timezones are whole hours — there are :30 and :45 offsets in the world in places like Canada, Australia, New Zealand, India, Iran, Afghanistan and Nepal. Dublin Mean Time used to be UTC-00:25:21 and Bombay Time used to be UTC+04:51. Malaya and Singapore used UTC+07:20 as daylight saving time until 1941. And so on.
JavaScript dates are always in UTC, but when you are presenting them, it uses the date's timezone and zoneinfo to determine the offset to use on the particular date that the date object reflects in the timezone of the runtime environment.
In Google Apps Script, certain properties of the runtime environment are set by the script project. The script project has a timezone of its own, separately from the hosting spreadsheet, and these two timezones may differ. That is why it is important to use Utilities.formatDate() and the spreadsheet's timezone when presenting a datetime value to the user.
Your code assumes the GMT+1 timezone, which may be different from the timezone of the spreadsheet. Most likely, your timezone is EET rather than GMT+1. These two are not the same thing. Currently the most important difference is that EET observes daylight saving time (EEST) while GMT+1 does not. It is possible that in the year 1899 there were other differences as well, which may explain the 24-minute discrepancy you describe.
To get the time value as it shows in the spreadsheet, you need to include the date component, and format the resulting datetime in the timezone of the spreadsheet, like this:
const dateString = wysylka.getRange('B5').getDisplayValue().replace(/^(\d+)-(\d+)-(\d+)$/, '20$3-$2-$1 ');
const timeString = wysylka.getRange('C5').getDisplayValue();
const datetime = new Date(dateString + timeString);
const timezone = SpreadsheetApp.getActive().getSpreadsheetTimeZone();
const pickupTimeString = Utilities.formatDate(datetime, timezone, 'HH:mm');
You may also want to check that your spreadsheet's File > Settings > Time zone is set correctly. Note that changing the timezone will change the presentation of datetime values in the spreadsheet, so they may need correction after you change the timezone.
Try this:
var wysylka = SpreadsheetApp.getActiveSheet();
var pickupTimeOld = wysylka.getRange("C5").getDisplayValue();
var tz = Session.getTimeZone(); // or SpreadsheetApp.getActive().getSpreadsheetTimeZone();
var today = Utilities.formatDate(new Date(), tz, 'yyyy-MM-dd');
var pickupTime = Utilities.formatDate(new Date(today + ' ' + pickupTimeOld), tz, 'HH:mm');
Probably in your case it makes sense to define today about this way:
var today = wysylka.getRange("B5").getDisplayValue()
Related
I observed something weird.
I am getting values from a sheet with a script.
Some value are dates like here.
When I read the value with:
function test2() {
var startCell = ss.getRange("B24");
var i = 0;
var val;
...
//Reading columns data
while (startCell.offset(0,i).getValue() != '') {
...
val = startCell.offset(-21,i).getValue();
i++;
}
...
};
When I look now at the value in the script debugger the value changed.
In that case there are additional 50min 39sec.
Am I doing something wrong?
Warm regards
Datetime conversion between systems is quite complex in the general case. For example, if the spreadsheet's timezone differs from the script project's timezone, there will be differences in what the datetime looks like in the spreadsheet and what it looks like when read and output by a script.
The weird looking 0:50:39 offset you mention could be because the region that currently observes the CEST timezone was back in the year 1900 using some local time that was offset by say 1 hour, 50 minutes and 39 seconds from GMT, instead of an offset that would be exactly one, two, three or so hours from GMT.
A well-documented example of these issues is the American railway time which had as many as 50 different local times across United States. The present US time zones were taken into use only in 1918.
Usually the easiest way to manage these differences is to use Utilities.formatDate() and the spreadsheet's timezone when producing human-readable outputs, like this:
function testDateOutput() {
const ss = SpreadsheetApp.getActive();
const date = ss.getRange('Sheet1!B24').getValue();
const timezone = ss.getSpreadsheetTimeZone();
console.log(Utilities.formatDate(date, timezone, 'yyyy-MM-dd HH:mm:ss'));
}
Note that the above method does not give perfect results for any and all moments and timezones because of the complexities mentioned above, especially with dates that are more than 50 years in the past.
See this answer for an explanation of how date and time values work in spreadsheets.
I'm reading a Google Sheet in javascript through Google Drive API.
I have this my function to get the last modification var dt = file.getLastUpdated() it's give me this result 4/5/2018 8:48:22 NOT my cuurent Time.
I used var formattedDate = Utilities.formatDate(file.getLastUpdated() , "GMT", "yyyy.MM.dd HH:mm:ss.SSS"); convert date to grenwich time but still the same issue.
I correct the Time Zone of my google script project to (GMT+01:00) Berlin and still wrong date time
You should be using "CET" in formatDate().
Utilities.formatDate(file.getLastUpdated() , "CET", "yyyy.MM.dd HH:mm:ss.SSS");
The formatDate() function takes 3 parameters: date, timeZone, and format. If you pass it "GMT", then the date/time will presented in that time zone. Instead, you need to pass it "CET" (for Central European Time) to have time displayed in that time zone.
EDIT: A potentially more stable way, that could better handle daylight savings (CET vs CEST), is to use Session.getScriptTimeZone(). This does require that you have your script time zone set, which you did correctly already. (There may not be a difference, as I don't know for sure how this is handled.)
Utilities.formatDate(file.getLastUpdated(), Session.getScriptTimeZone(), "yyyy.MM.dd HH:mm:ss.SSS");
check the time zone settings. (File > Spreadsheet Settings) Timezone is the second option.
I have a G Sheet script which is executed form Add-ons -> 'Add-on name -> 'do stuff'
During the add-on execution, I have to generate a date in user's time zone or at least in a spreadsheet time zone. I'm doing the following:
var timeZone = SpreadsheetApp.getActive().getSpreadsheetTimeZone();
var date = Utilities.formatDate(new Date(), timeZone, 'yyyy-MMM-dd HH:mm:ss z');
Problem I have, that SpreadsheetApp.getActive().getSpreadsheetTimeZone() gives me a wrong time zone than it's set in my spreadsheet settings.
Here is the flow. I create a new spreadsheet, by default, it's in Central European Time (CET) I run my add-on which should execute the above code and insert a date in the spreadsheet. However, I get the date in Pacific Standard Time (PST). When I go to the Spreadsheet settings and change the time zone and re-run my add-on I get exactly right time zone which is in the settings.
Any ideas what could be wrong?
Notes:
My add-on published privately.
SpreadsheetApp.getActive().getSpreadsheetTimeZone() - returns exactly America/Los_Angeles value in the case of PST
I am trying to write a script to add events from Spreadsheet to Calendar. I found the time of added events is always 1 hour off. I checked the time zone settings and here's what I found.
Calendar: (GMT-04:00) Eastern Time
Spreadsheet: (GMT-05:00) Eastern Time
Script: (GMT-05:00) Eastern Time
So they are all Eastern Time, but the Calendar uses the Daylight Savings Time while the Spreadsheet and Script.
I know I can manually adjust GMT every time DST changes so that the resulting time is right. But is there a better way, so that I can set and forget?
Instead of hardcoding the timezone with GMT-04:00 use the literal expression of your geographic area (aka Canonical ID) as defined in this standard : Joda.org
this is what Google Apps Script is using.
You can also get that more simply using
SpreadsheetApp.getActive().getSpreadsheetTimeZone()
or from the calendar object : calendar.getTimeZone()
all these parameters will handle daylight savings automatically, which is not the case for the GMT+xx:xx method.
You shouldn't have any problem as long as both are on the same timezone. Calendars have a setTimeZone(timeZone) method and spreadsheets have a setSpreadsheetTimeZone(timeZone) method so it is easy to synchronize both
EDIT
Following comment
Since you seem to combine date and time in a unique date object and that the daylight saving puts you into trouble (because "pure time" values in spreadsheets are converted to a date JavaScript object automatically and with summer time correction), below is a small code that I use to workaround this issue. I added a few logs to show intermediate values;
function combineDateAndTime(){
// in this example cell A1 has a date and cell B1 has time
var sh = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var dateOnly = sh.getRange('A1').getValue();
var timeOnly = sh.getRange('B1').getValue();
Logger.log('dateOnly object is : '+dateOnly+'\ntimeOnly object is : '+timeOnly);
var hours = Number(Utilities.formatDate(timeOnly, 'GMT'+timeOnly.toString().split('GMT')[1],'hh'));
var minutes = timeOnly.getMinutes();
var completeDateAndTime = new Date(dateOnly.setHours(hours,minutes,0,0));
Logger.log('completeDateAndTime is : '+completeDateAndTime);
}
Note : there are other ways to retrieve hour values, see this post for example.
I have a Google form to collect info on people leaving the organisation. One of the questions is 'What date and what time do they leave' The response is in the format dd/mm/yyyy, hh:mm. so a typical response would be 24/04/2015 17:00:00, and that's what I see in the Form responses 1 worksheet when the form is submitted.
I need to add the day of the week and copy the information into another worksheet within the spreadsheet, so I use
var leaveDate = inputSheet.getRange("G" + lastRow).getValue();
var leaveDateTime = Utilities.formatDate(leaveDate, "GMT", "EEE dd-MM-yyyy hh:mm:ss");
The issue I'm seeing is that when I paste the value the time is changing, and what gets pasted is
Fri 24-04-2015 04:00:00
Can anyone explain why this is happening and what I can do to resolve it?
Thanks
Apps Script converts date types to the time zone set up in the script editor. In the script editor, you need to set the time zone, even if you've set it in the spreadsheet.
Set the time zone to your local time zone.
Make sure the time zone in your spreadsheet, and the time zone in your script editor match.
If you have multiple editors of the spreadsheet in multiple time zones, and the other users have set the time to their local time, then obviously, their time and your time won't be the same. If someone left the organization on the other side of the world, at 4pm their time, they didn't leave the organziation at 4pm your time. You could assume that if the spreadsheet states 4pm, that it was their local time, and convert the time. In that case, you'd need to know the time zone of every time entry that is made, and then adjust it as necessary.
Try:
...
// var leaveDateTime = Utilities.formatDate(leaveDate, "GMT", "EEE dd-MM-yyyy hh:mm:ss");
var leaveDateTime = Utilities.formatDate(leaveDate, "GMT", "EEE dd-MM-yyyy HH:mm:ss");
...
UPDATE
Thanks to #sandy-good and #wchiquito for pointing me in the right direction. There were 2 issues. First issue is using hh instead of HH. The second issue is that we are on GMT+1 at the moment, and will be until end October, at which point we go back to GMT! As I don't want to code around that I'm going to simplify it by dropping the day, which then means I don't have to reformat the date.