Setting local date time for apps script triggers - javascript

The script timezone is set to Etc/GMT
I am accepting a date (using a datetime-local yyyy-MM-ddTHH:mm) from user to invoke a trigger function.
var at = moment(mydate); // say user sets mydate (local IST) as 2017-06-21T14:31
ScriptApp.newTrigger('onClock').timeBased().at(new Date(at)).inTimezone(CalendarApp.getDefaultCalendar().getTimeZone()).create();
this will invoke the trigger at 2017-06-21 20:01 local time IST
How do I make a code to invoke trigger at 2017-06-21T14:31 local time?

The inTimezone(timezone) method only accepts valid "long format" JODA time zones.
JODA.org
You are already getting the default calendar time zone:
CalendarApp.getDefaultCalendar().getTimeZone()
which returns the "long format" JODA type time zone, and that is what the inTimezone() method needs as it's parameter.
So, you don't need to get the 2017-06-21T14:31 information from the user, UNLESS their default calendar time zone is NOT the same as their local time. If your situation, is that there is a difference between the default calendar time zone, and their local time on their computer; and you want to account for that difference, then you do need to get the local time zone.
If you really do need to get a time zone that is different from the default calendar time zone, then either the user needs to select a time zone, or you can try to get it some other way.
In your question, you stated that the time you got is: 2017-06-21T14:31
That local time 2017-06-21T14:31 doesn't have time zone information in it. If it had the offset, then you could use the offset to work back to the "long format" time zone.
You could have a drop down list for the user to choose from, with values that are valid "long format" JODA time zones.
<select>
<option value="Etc/GMT+12">GMT +12</option>
<option value="Etc/GMT+11">GMT +11</option>
</select>
If you really do need to get the users local time zone from client side code, see the following Stack Overflow answer:
Stack Overflow answer - Getting the client's timezone in JavaScript
var offset = new Date().getTimezoneOffset();
console.log(offset);
Then you will need to convert the offset to a value "long format" time zone. You can do that by having a list "hard coded" into your code, and do a "look up" or construct a "Etc/GMT+##" time zone with code. The offset is the opposite of the GMT number. A standard offset of minus 12 is +12 GMT
Offset --- Long Format
-12:00 --- Etc/GMT+12

Related

Change timezone in my phpmyadmin shared server

I need to change my timezone to India timezone, but my shared server database showing some junk timezone I tried all the possibility but I can't get the India time zone.
I am using PHP Codeigniter
I Have a contact us page on my website, which saves all the send inquiry details to my mail and database. on that is showing some junk time on it
please help me out of this issue
In the case of shared hosting you would need to contact support guys or go for a live chat and they can change the default time zone for you.
You can use date_default_timezone_set() which sets a default timezone.
Please give a look here
OR
convert all your times to UTC on server side and just work with UTC instead
EDITED ANSWER
What you can do is :
Store your date time in DB in UTC, and while showing You can show Date time By getting current timezone of India.
// create a $dt object with the UTC timezone
$dt = new DateTime('2016-12-12 12:12:12', new DateTimeZone('UTC'));
// change the timezone of the object without changing it's time
$dt->setTimezone(new DateTimeZone('Asia/Kolkata'));
// format the datetime
$dt->format('Y-m-d H:i:s T');

Displaying Date and Time in JavaScript without timezone conversion

In my application there is a requirement to display date times in different time zones. All these timestamps are stored in the database in UTC.
If I send the UTC date time to client and display using JavaScript, it automatically converts it to the client's time zone.
For example:
//the string is what I receive from the server
var date = new Date('2019-05-03T09:30:00.000+05:30');
document.write(date.toLocaleString("en-US"));
displays 5/2/2019, 11:00:00 PM in the browser which is in the Central Time Zone.
What if I do not want this automatic change, and just want to display the date and time that I received from the server?
I know I can get the offset from the date object, but then I have to do additional processing to display the same value that I already got from the server!

Retrieve rows from SQL database after certain time

I have a table in my database that holds events. The events have a start time and end time. But when they are inserted into the database the time the user inputed is converted to UTC time. So for me, my time offset is -4 hours from UTC time and if I input the start time as 7pm (19:00:00) that is converted to 23:00:00 (UTC) when added to the database.
The Problem
I want the user to be able to select all events at and after 7pm until the end of the day, but If I do the query: WHERE time > 23:00:00 it will only retrieve event where the start time is between 7:00pm and 7:59pm. At 8:00pm the UTC time in the database is 00:00:00 which is less than 23:00:00.
The field type that the start time is stored in is time
I use javascript to get the time-offset and input, and php for mySQL. Any ideas on how to solve this using those languages or SQL?
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_convert-tz
When you pass in your local time, you can wrap it in this conversion function to convert it to UTC time. This is a mysql function, so all the information transform can happen on the server.

How to get correct time in utc using moment.js when the device time is set incorrectly?

I set the mobile device time forward/backward say by 10 minutes and did moment.utc() to get utc timestamp. The received timestamp is not the correct one. It preserves forward/backward minutes.
Any solution?

Javascript dates passed to webservice running linq to sql issues

When passing a date in javascript, that has a 00:00 time, to a webservice that is using linq to sql to access the DB, on some clients browsers, after the save, the date that was passed to the datetime column in the db now has a time that is no longer 00:00, some cases it is hours before midnight so the date is moved into the previous days date, other times it is hours before.
How do we always make the date save and not the time to the db?
This is using asp.net webforms and c# and asmx webservices and most queries are compiled.
It depends on the details of how the date is being encoded. At a high level though, you have to:
Make sure the timezone doesn't get wiped out when it's sent from client to server. That means, send it as either a date-only string, or as a date with the timezone preserved; not as a UTC date.
Make use of DateTimeKind (UTC or local) and/or DateTimeOffset on the server to ensure it is properly represented whenever you're sending/receiving to the client/database.
See also here: Linq to SQL DateTime values are local (Kind=Unspecified) - How do I make it UTC?
I'm guessing that the date and time on the client is important. In that case how about converting the date-time info to iso format before sending it to the server and also send the client's timezone offset as well.
var d = new Date
d.toISOString() // 2012-05-05T22:14:35.506Z
// or maybe jus
d.getTime() // milliseconds since jan 1st 1970 or thereabouts
d.getTimezoneOffset() // Timezone offset in minutes from UTC
This way you get the UTC date and time and the timezone offset as well, that is how many minutes UCT time differs from local time. For example Norway would have a negative offset (UTC-Norwegian time = negative).

Categories

Resources