Event time on Google analytic - google-analytics

<script type="text/javascript">
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
var timeStamp = (hours + ":" + minutes + " ");</script>
onClick="_gaq.push(['_trackEvent', 'cata', 'act', 'label','timeStamp '])"
I am trying to pass the current time as a value but i cannot get it work
any idea

because the value parameter should be number not string!

Related

Invalid Date in moment.js datetime addition

I want to add time/duration in HH:mm(eg 00:10) to a Date time in format MM-DD-YYYY HH:MM
I have date and time in 2 separate objects so am trying the below
var plannedStartDate = document.getElementById("date1"); //eg 02-12-2020
var plannedStartTime = document.getElementById("plannedStart1"); //eg 09:00
var plannedStartDateTime = moment(plannedStartDate.value + " " + plannedStartTime.value);
var minutes = $("#duration1").text().split(':')[1];
var hours = $("#duration1").text().split(':')[0];
var date = plannedStartDateTime.add(hours, 'hours').add(minutes, 'minutes').format("MM-DD-YYYY HH:mm");
console.log("Final:"+date); //gives invalid date
What am I doing wrong
Check this working sandbox: https://codesandbox.io/s/compassionate-bas-fg1c2
It adds the hours and minutes to the input date and then prints in a given format
var moment = require("moment");
var input = "2020-02-12";
var hoursMinutes = "9:10";
var hours = hoursMinutes.split(":")[0];
var minutes = hoursMinutes.split(":")[1];
var momentInTime = moment(input)
.add(hours, "hours")
.add(minutes, "minutes");
var converted = moment(momentInTime).format("DD-MM-YYYY HH:mm:ss");
console.log(converted);

Is there a way to use a script to pull a date from a Google Form submission from sheets into Gmail?

I have no experience with script writing, but I was able to find a script and edit it (with lots of trial and error) to fit my need.
I have a Google Form where the first question allows users to select a date, but it is not necessarily the date users are completing the form. The results export to a Google Sheet, and I have a script that sends an email with the form responses.
It worked beautifully until Daylight Savings Time. Now, the dates in the spreadsheet are correct, but in the emails they are one day off.
Example email message:
Your child, NAME, received a dress code violation on Wed Mar 27 2019
23:00:00 GMT-0600 (CST), for No ID.
Before Daylight Savings Time, the time was showing as 00:00:00.
In the code, row[2] is the date pulled from the spreadsheet.
var EMAIL_SENT = "EMAIL_SENT";
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Script");
var startRow = 2;
var numRows = 5000;
var dataRange = sheet.getRange(startRow, 1, numRows, 5000)
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[12];
var message = "Your child, " + row[10] + ", received a dress code violation on " + row[2] + ", for " + row[11] + ".\nIf you have any questions, please email NAME at name.name#name.org\n\nThank you,\n\nNAME\nAssistant Principal";
var emailSent = row[13];
if (emailSent != EMAIL_SENT) {
var subject = "Uniform Violation - Do Not Reply";
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 14).setValue(EMAIL_SENT);
SpreadsheetApp.flush();
}
}
}
Ideally, the email would provide the date exactly from the spreadsheet in MM/DD/YYYY format.
Instead, the emails show the previous day with the time of 11 pm.
Try this:
var EMAIL_SENT = "EMAIL_SENT";
function sendEmails2() {
var ss=SpreadsheetApp.getActive();//added this
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Script");
var startRow = 2;
var numRows = 5000;
var dataRange = sheet.getRange(startRow, 1, numRows, 5000)
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[12];
var message = "Your child, " + row[10] + ", received a dress code violation on " + Utilities.formatDate(new Date(row[2]),ss.getSpreadsheetTimeZone(), "MM dd, yyyy HH:mm:ss" ) + ", for " + row[11] + ".\nIf you have any questions, please email NAME at name.name#name.org\n\nThank you,\n\nNAME\nAssistant Principal";//modified this
var emailSent = row[13];
if (emailSent != EMAIL_SENT) {
var subject = "Uniform Violation - Do Not Reply";
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 14).setValue(EMAIL_SENT);
SpreadsheetApp.flush();
}
}
}
You will probably have to change the dates format.
Date Format

Code to parse a local date, time, and timezone into a UTC string with Moment.js

In separate fields, I collect DisplayDate, DisplayTime, and TimeZone from the user. I want to put those into a moment and output the UTC formatted string to save into a hidden field that gets sent back to the server. I used the below code, but it uses the local timezone, rather than the selected TimeZone I entered. How do I get it to observe selTimeZonesVal?
var startTime = $('#StartTime');
var displayDateVal = $('#DisplayDate').val();
var displayTimeVal = $('#DisplayTime').val();
var selTimeZonesVal = $('#TimeZones').val();
var dtMoment = moment(displayDateVal + ' ' + displayTimeVal).tz(selTimeZonesVal);
var formattedUtc = dtMoment.utc().format('YYYY-MM-DDTHH:mm:ss');
startTime.val(formattedUtc);
The problem was with the date parsing. Somehow, moment was able to parse the date, but it would ignore the timezone if the date wasn't in ISO format.
The fix:
var startTime = $('#StartTimeUtc');
var displayDateVal = $('#DisplayDate').val();
var displayTimeVal = $('#DisplayTime').val();
var selTimeZonesVal = $('#TimeZones').val();
// Massage the date so moment can parse it (moment doesn't like mm/dd/yyyy)
var localDT = new Date(displayDateVal + ' ' + displayTimeVal);
var parseDT = moment(localDT).format('YYYY-MM-DDTHH:mm:ss')
var dtMoment = moment.tz(parseDT, selTimeZonesVal);
var formattedUtc = dtMoment.utc().format('YYYY-MM-DDTHH:mm:ss');
startTime.val(formattedUtc);

Moment parse strange behavior with hours

I would like to understand why moment.js is changing minutes to "04" every time I parse, what am I doing wrong?
var sDate = '2017-04-24';
var sHour = '16:54:10';
alert(moment(sDate + ' ' + sHour).format('DD/MM/YYYY HH:MM:SS'));
alert(moment.utc(sDate + 'T' + sHour).format('DD/MM/YYYY HH:MM:SS'));
alert(moment(sDate + ' 00:00:00').format('DD/MM/YYYY HH:MM:SS'));
<script src="https://momentjs.com/downloads/moment-with-locales.min.js">
Accordinly to the official docs it should work.
Moment tokens are case sensitive, please note that uppercase M stands for months while you have to use lowecase m for showing minutes.
See formatting docs.
Note that you probably have to change SS (fractional seconds) to ss (seconds) too.
Your code will be like the following:
var sDate = '2017-04-24';
var sHour = '16:54:10';
alert(moment(sDate + ' ' + sHour).format('DD/MM/YYYY HH:mm:ss'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

How to Perform Timing Subtraction using Moment Js?

Iam trying to perform Subtraction in Momentjs:-
// Find the duration between two dates
var breakfast1 = moment('11:32','HH:mm');
var lunch1 = moment('12:52','HH:mm');
alert( moment.duration(lunch1 - breakfast1).humanize() + ' between meals' ); // 1 hours between meals
Output is alerting Only Hours .But How can I get "01:20"as Output Instead of 1 hour Any Ideas ?
how to generate Minutes along with Hours
.humanize() will always round to a specific interval. Break your code up and manually create the display you want...
var breakfast1 = moment('11:32','HH:mm');
var lunch1 = moment('12:52','HH:mm');
var dur = moment.duration(lunch1 - breakfast1);
alert( dur.hours() + ":" + dur.minutes() + ' between meals' );

Resources