Date of Joining calculation not working with moment 2.19.1 - momentjs

Below code not working with "moment": "^2.19.1",
var a = moment([2017, 12, 09]);
var b = moment([2011, 02, 17]);
var years = a.diff(b, 'year');
b.add(years, 'years');
var months = a.diff(b, 'months');
b.add(months, 'months');
var days = a.diff(b, 'days');
console.log(years + ' years ' + months + ' months ' + days + ' days');
Please help.

You can use moment duration format.
Include moment duration format plugin, and then..
var duration = moment.duration(a.diff(b));
console.log(duration.format('y [years] m [months] d [days]'));

Related

How to get all dates and Days from Week Number?

I have 7 buttons with text current Week's Dates, Month, Day. On Click of something need changes the Dates, Month, Days to next Week of the month/next month.
$("#nextweek").click(function () {
debugger;
var lastDayinWeek = $("#ts_timeentry").val();
var lastDayinWeekInDateFormat = new Date(lastDayinWeek);
var dd = lastDayinWeekInDateFormat.getDate()+7;
var mm = lastDayinWeekInDateFormat.getMonth();
var yyyy = lastDayinWeekInDateFormat.getFullYear();
if (dd < 10) dd = '0' + dd;
if (mm < 10) mm = '0' + mm;
lastDayinWeekInDateFormat = mm + '/' + dd + '/' + yyyy;
$("#ts_timeentry").val(lastDayinWeekInDateFormat);
});
I tried adding days, but getting invalid Date after end of the month

Working with Date, Time in Google Apps Script

I need help with some quick coding with google apps script, linking to my googlesheets spreadsheet.
In the googlespreadsheets, I have a cell with the value “26-Jun-2020”. It is a date.
I want to use google apps script to calculate the number of days difference between that date (“26-Jun-2020”) and today’s day, but it won’t do the calculation for me somehow.
If I print only “expiry_date[i]” using Logger.log(expiry_date[i]), it will provide the output “Fri Dec 17 2021 01:00:00 GMT-0500 (Eastern Standard Time) “
function Put_Options_Expiry_Alert() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Long equity (sell puts)");
//var timeZone = AdsApp.currentAccount().getTimeZone(); //Get timezone of current spreadsheet
var status = sheet.getRange("F:F").getValues();
var expiry_date = sheet.getRange("M:M").getValues();
var potential_capitaloutlay_USD = sheet.getRange("Z:Z").getValues();
Logger.log("Length of status = " + status.length);
Logger.log("Length of expiry_date = " + expiry_date.length);
Logger.log("Length of potential_capitaloutlay_USD = " + potential_capitaloutlay_USD.length);
for (var i = 0; i < status.length; i++) {
if (status[i] == "Entered") { //Evaluate if this is a live Put position
//Calculate the time difference of two dates using date2. getTime() – date1. getTime();
//Calculate the no. of days between two dates, divide the time difference of both the dates by no. of milliseconds in a day (1000*60*60*24)
Logger.log("expiry date is = "+expiry_date[i]);
Logger.log("today's date is = "+Date());
var days_to_expiry = dateDiffInDays(expiry_date[i],Date());
Logger.log(days_to_expiry);
}
}
}
// Function that returns the number of days difference between DateA and DateB
// DateA and DateB are javascript Date objects
function dateDiffInDays(DateA, DateB) {
var milliseconds_per_day = 1000 * 24 * 60; // number of milliseconds in a day
const utcA = Date.UTC(2021, DateA.getMonth(), DateA.getDate());
const utcB = Date.UTC(2020, DateB.getMonth(), DateB.getDate());
return Math.floor((utc2 - utc1) / milliseconds_per_day);
}
function timeDiffDays(Start, End) {
var day = 86400000;
var t1 = new Date(Start).valueOf();
var t2 = new Date(End).valueOf();
var d = t2 - t1;
return Math.floor(d / day);
}

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);

How format api wordpress date in flutter

I work on flutter api wordpress project . How format date from wordpress api . date came in this format (2019-08-26T16:23:06) . i want to remove second and year . and month like (June , April)
you can use https://pub.dev/packages/intl and DateFormat :
DateTime dateTime = DateTime.parse(data[index]['date']);
String dateformat = DateFormat( "dd.MM.yyyy HH:mm").format(dateTime);
You can create a DateFormat object that fit your needs and use it to format your date.
Head over to the documentation to examples: https://api.flutter.dev/flutter/intl/DateFormat-class.html
I solve my problem with this function
timenow() {
var day = DateTime.parse(
snapshot.data[index].date)
.day;
var month = DateTime.parse(
snapshot.data[index].date)
.month;
var year = DateTime.parse(
snapshot.data[index].date)
.year;
var hour = DateTime.parse(
snapshot.data[index].date)
.hour;
var min = DateTime.parse(
snapshot.data[index].date)
.minute;
return year.toString() +
"/" +
month.toString() +
"/" +
day.toString() +
" " +
hour.toString() +
":" +
min.toString();
} // format date and time

Displaying Date and Time information in script with format from spreadsheet

I'm working on a basic email-sending script in Google Sheets. I want to send email reminders on clicking a button, reminding people of an appointment at a date and time along with the location.
Everything works, except for the date is displayed in a confusing long form format:
The data shown in the date and time cells is:
8/27/2018
10:30 AM
The data being sent in the email is:
Mon Aug 27 2018 15:00:00 GMT+0800 (HKT)
Sun Dec 31 1899 02:30:00 GMT+0800 (HKT)
I only need to show the date and time as written in the sheets. Is there a way to do this?
Another question is that one person might have more than one appointment, can I combine all appointments date/time for each unique email address into one email instead of multiple emails?
Adding code below:
function sendArticleCountEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setActiveSheet(ss.getSheetByName("SSname"));
var sheet = SpreadsheetApp.getActiveSheet();
var dataRange = sheet.getRange("B2:O2");
var data = dataRange.getValues();
for (i in data) {
var rowData = data[i];
var emailAddress = rowData[0]; //email
var recipient = rowData[1]; //name1
var message2 = rowData[2]; //Name2
var message3 = rowData[3]; //type
var message4 = rowData[4]; //Appt Date
var message5 = rowData[5]; //Appt Time
var message6 = rowData[6]; //Appt type
var message7 = rowData[7]; //Notes
var message8 = rowData[8]; //Facility Name
var message9 = rowData[9]; //Facility Address
var message10 = rowData[10]; //City
var message11 = rowData[11]; //Zip
var message12 = rowData[12]; //Phone
var message13 = rowData[13]; //Service
var message = 'Dear ' + recipient + ',\n\n' + 'This is a friendly reminder of your assignment tomorrow with:' + '\n\n' + message2 + ' at ' + message5 + '.'
+ '\n\n' + 'The assignment is located at ' + message8 + ', ' + message9 + ', ' + message10 + ', ' + message11 + '.' + '\n\n'
+ 'This assignment is noted as a ' + message6 + ' ' + message3 + ' ' + message13 + '. ' + message7 + '\n\n';
var subject = 'Reminder: ' + message4 + ' ' + message5 + ' (' + message2 + ')';
MailApp.sendEmail(emailAddress, subject, message);
}
}
I think the problem is here, using either var or rowData. The row is correct, just it doesn't pull the data in as shown in Sheets.
var message4 = rowData[4]; //Appt Date
var message5 = rowData[5]; //Appt Time
There is no time in the DATE cell (8/27/2018), so I'm guessing it defaults to "15:00:00" and because my data is input by mailparser, I don't have a method to change the format of the data. Rather I hope I can use the data as displayed in Google Sheets and display this into the Gmail.
I appreciate your suggestions in advance, thanks!

Resources