I am having a weird issue with dates formatting in Google Sheets and Google Script.
I am developing a script that reads a list of dates in a Google sheet and then sends automatic emails to a list of recipients. I have a cell in the spreadsheet with a date formatted as "dd/mm/yyyy" and then I use the following formulas (cell + 7, cell + 14, cell + 21, cell + 28, etc...) to generate all the other dates in the spreadsheet.
I use the following code to read the dates in the spreadsheet:
var sheet = SpreadsheetApp.getActiveSheet();
var drng = sheet.getDataRange();
var rng = sheet.getRange(4, 2, drng.getLastRow()-3, drng.getLastColumn());
var rngData = rng.getValues();
var todayDate = Utilities.formatDate(new Date(), "GMT+1", "dd/MM/yyyy");
// CYCLE THROUGH DATA
for (var i = 0; i < rngData.length; i++) { // cycle through the rows in the spreadsheet
var ptLastName = rngData[i][0]; // fixed patient data used to email customization
var ptFirstName = rngData[i][1];
var ptDoB = Utilities.formatDate(rngData[i][2], "GMT+1", "dd/MM/yyyy");
var ptSex = rngData[i][3];
var ptEmail = rngData[i][4];
var ptDoS = Utilities.formatDate(rngData[i][5], "GMT+1", "dd/MM/yyyy");
var ptDoD = Utilities.formatDate(rngData[i][6], "GMT+1", "dd/MM/yyyy");
for (var j = 7; j <= 14; j++) { // cycle through follow-up data for the current patient
var currentDate = Utilities.formatDate(rngData[i][j], "GMT+1", "dd/MM/yyyy");
if (currentDate == todayDate) { // send email with the questionnaire
writeEmail(ptFirstName, ptLastName, ptSex, ptDoD, ptEmail);
rng.getCell(i+1, j+1).setBackground("yellow");
}
}
}
The issue is that when I try for read and format some of the dates they are not corresponding to the ones listed in the spreadsheet. I have noted that the wrong dates in the code are systematically 1 day earlier than the ones listed in the spreadsheet. On a closer look I have noted that some of the dates read from the spreadsheet at GMT+2 while others are GMT+1.
That's weird because I have checked my system settings (it GMT+1) as well as the spreadsheet settings (GMT+1). So I don't understand why some dates are automatically converted to GMT+2.
I tried it this way and it works okay:
function dattest() {
const ss=SpreadsheetApp.getActive();
const sh=ss.getActiveSheet();
const rg=sh.getDataRange();
const vs=rg.getValues()[0];
vs.forEach(function(d){
Logger.log(Utilities.formatDate(new Date(d),Session.getScriptTimeZone(),"dd/MM/yyyy"));
});
}
Display Values:
8/1/2020,8/8/2020,8/15/2020,8/22/2020
Formulas:
,=A1+7,=A1+14,=a1+21
The output from view Executions:
Aug 1, 2020, 2:52:37 PM Info 01/08/2020
Aug 1, 2020, 2:52:37 PM Info 08/08/2020
Aug 1, 2020, 2:52:37 PM Info 15/08/2020
Aug 1, 2020, 2:52:37 PM Info 22/08/2020
Seems correct to me
thank you for your help.
I finally found out what the problem was.
The dates in the spreadsheet were stored as dd/mm/yyyy, and the system automatically added 00.00.00 as time to them. As a result, when I was trying to read some of the dates with the code some of them were converted as the day before because of the daylight saving time adjustment. I have managed to solve the issue adding 1/3 of a day to all the dates. This way they are stored as dd/mm/yyyy 8.00.000 am and they remain the same day all year long.
Related
When converting time to UTC its showing one hour less than expected
I am updating a variable of dot net via moment to convert the time & show local system time to user. But post conversion i am getting one hour less. Tried utcOffset but getting error utcOffset is not a function. any suggestion
Where formData.SubmittedDate = "6/7/2019 5:44:59 AM"
$('[data-utcdate]').each(function () {
var d = moment($(this).attr('data-utcdate'));
//var isDST = d.utc().local().isDST();
//var d = moment(d).utcOffset(d);
d = d.utc();
$(this).html(d.format('MMM D, YYYY h:mm A'));
})
Getting :Jun 7, 2019 12:14 AM
Expected : Jun 7, 2019 11:44 AM
From the docs:
Get the UTC offset in minutes.
So you could use a manipulation method like add with it:
$('[data-utcdate]').each(function () {
var d = moment($(this).attr('data-utcdate'));
var offset = d.utcOffset() // will return the offset in minutes
var time = d.add(offset, "m");
$(this).html(time.format('MMM D, YYYY h:mm A'));
})
The ISO week numbers are not right when trying to display them on the x-axis using tickformat.
var parseDate = d3.time.format("%Y%W").parse;
var x = d3.time.scale().range([0, width]);
x.domain([parseDate("201552"), parseDate("201602")]);
var xAxis = d3.svg.axis().scale(x)
.orient("bottom")
.ticks(d3.time.monday,1)
.tickFormat(d3.time.format("%W"))
This works fine with most of the scenario except with years that have 53 weeks.
Example: from week 201552 to week 201602. In this scenario it always skips week 53 so the the tick I get are [52, 01, 02]. although the expected output is [52,53,01,02]. Here is my code
I also tried parsing the date string using momentjs since I face this problem skip week in d3.time.format
This is the modified code using momentjs to parse the date:
var parseDate = function(d){
return moment(d, "YYYYWW").toDate();
}
x.domain([parseDate("201552"), parseDate("201602")]);
var MultiFormat = d3.time.format.multi([
["%W", function(d) {
return moment(d).isoWeek();
}],
]);
var xAxis_weeks = d3.svg.axis().scale(x)
.orient("bottom")
.ticks(d3.time.monday,1)
.tickFormat( function(d) {return MultiFormat(d);})
Now the output is [51,52,01,02] which also should have been [52,53,01,02]
I am trying to return the month number passing the month name using MomentJS. For example if I pass "July" to moment() I would expect 7 to be returned.
After reading through the docs I tried several different ways, and this way came close...
console.log(moment().month("July"));
In the console, buried in the response I could see this...
_monthsParse: Array[7]
Could anyone please tell me how to return the month number using MomentJS correctly?
Try :
moment().month("July").format("M");
Relevant documentation: http://momentjs.com/docs/#/get-set/month/
alert(moment().month("July").format("M"));
<script src="https://momentjs.com/downloads/moment.min.js"></script>
Anybody looking to get month name from month number then you can try :
const number = 1; // 0 = Jan & 11 = Dec
moment().month(number).format("MMM"); // Feb
Use following to get full month name :
const number = 1; // 0 = January & 11 = December
moment().month(number).format("MMMM"); // February
To use simple month number try this:
const month = 2 //Feb
moment(month, 'M').format('MMMM');
##get month name in moment js with node js
moment() give today date
format("DD-MMMM-YYYY") / output 18-May-2020
format("DD-MM-YYYY") / output 18-05-2020
- sperator you can use /
```
var moment = require('moment');
m_date = moment().format("DD-MMMM-YYYY");
console.log("moment date :", m_date)
```
##output
```
moment date : 18-May-2020
```
Read Officail Docdescription here
var d1 = Date.parse(date); // eg, 7th jan
var d2 = Date.today(); // eg, 2nd jan
I want to do:
(d2 - d1).days
which with the above data, should return 5.
Is this possible with datejs?
The optional TimeSpan module includes functionality to easily get various value differences between two date objects.
http://code.google.com/p/datejs/source/browse/trunk/src/time.js
Just include after your main date.js include, or combine.
In the "time.js" file, there's also a TimePeriod class. You might not require, so just crop what you need if weight is an issue.
The following sample demonstrates the full scenario.
Example
var d1 = Date.parse("jan 7"); // eg, 7th jan
var d2 = Date.parse("jan 2"); // eg, 2nd jan
console.log(new TimeSpan(d1 - d2).days); // 5
Hope this helps.
I have a date/time field from a shopping cart API feed, but I don't know what format it is in and I don't have access to the database.
What could [1252457867] be for a date?
These dates are all within the last couple weeks
Any ideas?
Clearly a unix timestamp.
1252457867 = 09 Sep 2009 - 02:57:47
This sounds like seconds since the Unix Epoch (January 1, 1970).
That looks like seconds elapsed since Jan. 1st, 1970 12:00AM.
Use this function to get the date:
var baseDate = new DateTime(1970, 1, 1, 0, 0, 0);
var transactionDate = baseDate.AddSeconds(1252457867);
This will output {9/9/2009 12:57:47 AM} PST
**EDIT: **
If you need UTC:
var utcDate = baseDate.AddSeconds(1252457867).ToUniversalTime();
This outputs {9/9/2009 7:57:47 AM}
--Adam