Google Spread Sheet Script. Working with Dates - google-sheets

I am reading list of Google Sheet cells containing DateTime, using Google Apps Script.
The values in the cells are:
A1: Jul 26 13:00
A2: Jul 27 0:00
var dateValues = SpreadsheetApp.getActiveSheet().getRange("A1:A2").getValues();
However, values read are 1 hour behind. This is what I see in the debugger:
dateValues[0] = Wed Jul 26 2017 12:00:00 GMT+0200 (EET)
dateValues[1] = Wed Jul 26 2017 23:00:00 GMT+0200 (EET)
I guess this is a time zone issue, but I don't really understand the concept.
My time zone is currently (due to DTS) GMT + 3. Indeed, outside the DTS period it is GMT +2. The spread sheet time zone is Jerusalem GMT+2.
EET - don't underrated why it is being used.
Basically, I would expect to get in code the values with in the sheet.
What is the concept?

there are two ways to solved this
Use getDisplayValues() rather than getValues(). This will force to do some conversions, as getDisplayValues() returns strings not dates
make you script editor time zone match the sheet tz.
In my case the sheet was (GMT+2 Jerusalem), but the script editor was different (GMT+2 Moscow) for some reason.
Setting the script editor TZ, solved the problem.

Related

Daylight saving issue Nov 04 - 05 in EST location

Date difference is zero today. When comparing Nov 04 and Nov 05, rare issue happening only for today.
Code I'm using
let dayDiff = Calendar.current.dateComponents([.day], from:businessDate , to: today).day!
Ipad date and time set to automatically.
Tried restarting iPad.
Tried setting date to tomorrow and I get date diff 1.
Is there any issues because of changes in day light savings yesterday?
If so, what settings I have to update in order to work it as expected?
Note: We can fix it with code changes, but like to know any iPad settings will fix this problem.
In any timezone that had a change in daylight saving time during that period (such as the in USA), the difference between 2018-11-04 00:00:00 UTC and 2018-11-05 00:00:00 UTC is only 23 hours which is less than 1 day so you get a difference of 0 days.
If you create those dates with midnight local time instead of UTC time, you will get the expected result of 1 day difference.
Or if you use a calendar set to the UTC timezone to calculate the difference, you will get 1 day difference.

Filter dates December text format D Google Sheets

I have a column of dates that I want to convert to the following format below:
Oct 1,5,10,12,18,19,26,27,28,29,30
Nov 2,3,4,9,17,18
Dec 3,22,23,24,25,26,27,28,29,30,31,30,30,30,30,30,30,30,30,30
Jan 1,2,3,4,5,6,16,18,19,26
Feb 2,9
Mar 2,8,9,10,23
Apr 13,19,20,21,22,27
I'm using e.g.
=join(",",(filter(text($H$38:$H$204,"d"),month($H$38:$H$204)=10)))
where $H$38:$H$204 refers to the dates to produce the days for October and other months which all work fine with the exception of December (12) which produces repetitive 30 at the end.
It's bizarre behaviour and I can't work it out.
ok got it thanks.
if the array has blank cells at the end it seems to produce this behaviour.
Use:
=join(",",(filter(text($H$38:$H$204,"d"),$H$38:$H$204<>"",month($H$38:$H$204)=12))) to filter out blank cells also.
Still bizarre it seems to happen for only one month.
Regards

How to work with raw date format in Google Sheets

I'm using Zapier to input timestamps of emails to Google Sheets. The format I'm seeing in Google Sheets is:
Wed, 28 Jun 2017 21:02:51 +0000 (UTC)
Is there a way to either change this (in Google Sheets) to 2017-06-28 21:02:51 or to help date-related functions to understand this format?
You should be able to extract date from string:
=DATEVALUE(REGEXEXTRACT("Wed, 28 Jun 2017 21:02:51 +0000 (UTC)","\d+ [A-Za-z]{3} \d{4}"))
REGEXEXTRACT part extracts "28 Jun 2017". Then format the result as date.
Edit:
Getting closer :) Error DATEVALUE parameter '28 Jun 2017' cannot be
parsed to date/time.
I think this is bacause of your regional settings.
Option: writing script, my solution wont work.
Option: Change File → Regional Settings to US.
Option: modify the formula so it replaces english names of monthes into their numbers. This would produce ugly big formula.

Moment JS Show Local Time from DateTime.UTCNow

I have a JSON method which is returning a UTC DateTime I am storing on the server. It returns the following:
/Date(1394155885817)/
I'm trying to figure out how to get Moment JS to show me the local (browser) time. So my timezone is -5 (EST) I'd want to see 3:31 instead of 8:31. See the fiddle below.
http://jsfiddle.net/R9UbS/
What am I doing wrong here? How can I force Moment to return local?
I just ran the fiddle in Romania and got 3:31. Also, javascript's Date constructor handles UTC millis correctly. You're getting 8:31 which makes me think it's doing what it's supposed to do, showing the local time. Why is 8:31 incorrect? And is your device's timezone set correctly? (Btw if your timezone is GMT -5 then you would indeed get 8:31 PM the previous day)
Just to put this a bit in perspective, i put the number in a milliseconds to time converter and i saw that 1394155885817 actually means Fri Mar 07 2014 1:31:25 AM in UTC (or GMT for convenience) - subtracting 5 hours from this would result in Fri Mar 06 2014 8:31:25 PM

Rails: how to create Time object in specific time zone

My app is working in "Moscow" (+04:00) timezone. But sometimes I need to create time object by only local time (for example "01 may 2012 13:45") and name of ActiveSupport::TimeZone object (for example "Berlin": +02:00 in Summer Time and +01:00 otherwise).
For example if I get "01 may 2012 13:45" and "Berlin" as input I want to yield "2012-05-01 13:45:00 +0200" or "2012-05-01 11:45:00 +0000". I create following function:
def from_local_datetime(local_datetime, time_zone)
offset = Time.now.in_time_zone(time_zone).formatted_offset
datetime = case local_datetime
when String
DateTime.parse(local_datetime)
else
DateTime.new(local_datetime)
end.change(:offset => offset)
return datetime
end
And at the first look it works as I expected. But is it a best practice for this kind of task? May be in some situation It works with errors. I'm not definitely sure.
I would be greatful to any comments.
UPD: I think bug may occur about time when DST changing the time. For example 26 march 2011 was GMT+1 in Berlin time zone and Time.now.in_time_zone("Berlin").formatted_offset returns "GMT+1", but it would be GMT+2 in 27 march 2011. So if I call from_local_datetime("28 march 2011", "Berlin") before 27 march it returns 28 march 2011 00:00:00 +0100, but If I call it after changing the time my function returns 28 march 2011 00:00:00 +0200 :(
Your conversion method is the right approach.
With web sites, you should make sure times are stored as UTC in the database. If you can get the UTC value out of the database, instead of the local time (or maybe you can set your web server's time zone to UTC) it won't have to convert the time from UTC to local time, when you are going to then convert it to the user's timezone anyway.
And, of course, you will have to store the user's time zone preference.
TZInfo::Timezone.get('Europe/London')
Find the time zone
http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html

Resources