Momentjs add doesn't work - momentjs

var startDate = moment("2017-06-30 00:00 +0000", "YYYY-MM-DD HH:mm Z");
var endDate = startDate.clone();
endDate.add(2, 'days');
console.log('startDate', startDate);
console.log('endDate', endDate);
I'm trying to add 2 days to the startdate but the endDate remains the same as the startdate. Am I doing something wrong?

To add days you can simpley use moment().add(2) here 2 is the number of days you wish to add
var startDate = moment("2017-06-30 00:00 +0000", "YYYY-MM-DD HH:mm Z");
var endDate = startDate.clone();
endDate.add(2); // or use endDate.add(2, 'day')
console.log('startDate', startDate);
console.log('endDate', endDate);
<script src="https://momentjs.com/downloads/moment.js"></script>
If you like to add months or year just give the second parameter as months or years
var startDate = moment(); // assign today
console.log('After 2 days:', startDate.add(2, 'days')); // startDate.add(2, 'day')
console.log('After 2 months:', moment().add(2, 'months')); // or startDate.add(2, 'month')
console.log('After 2 years:', moment().add(2, 'years')); // or startDate.add(2, 'year')
<script src="https://momentjs.com/downloads/moment.js"></script>

Related

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

Need first day of last month and Last day of next month from current date in Asp.Net

Need first day of last month and Last day of next month from current date in Asp.Net
Eg : Current date 02/12/2019
first day of last month = 01/11/2019
Last day of next month = 31/01/2020
var myDate = DateTime.Now;
var firstDateOfLastMonth = myDate.AddMonths(-1);
firstDateOfLastMonth = firstDateOfLastMonth.AddDays(-firstDateOfLastMonth.Day + 1);
var lastDayOfNextMonth = myDate.AddMonths(1);
lastDayOfNextMonth = new DateTime(lastDayOfNextMonth.Year, lastDayOfNextMonth.Month, DateTime.DaysInMonth(lastDayOfNextMonth.Year, lastDayOfNextMonth.Month));
You may have to refer DataTime C# for more info
please try below line of code:
DateTime lastMonthdate = DateTime.Now.AddMonths(-1);
var lastMonth = new DateTime(lastMonthdate.Year, lastMonthdate.Month, 1);
var lastDayOfNextMonth = lastMonth.AddYears(1).AddMonths(1).AddDays(-1);

Format datetime to YYYY-MM-DD HH:mm:ss in moment.js

I have a string in this format:
var dateTime = "06-17-2015 14:24:36"
I am using moment.js and I am trying to convert it into YYYY-MM-DD HH:mm:ss -> 2015-06-17 14:24:36.
I have tried this method
dateTime = moment( dateTime, 'MM-DD-YYYY HH:mm:ss',true).format("YYYY-MM-DD HH:mm:ss");
But getting dateTime as Invalid Date.
const format1 = "YYYY-MM-DD HH:mm:ss"
const format2 = "YYYY-MM-DD"
var date1 = new Date("2020-06-24 22:57:36");
var date2 = new Date();
dateTime1 = moment(date1).format(format1);
dateTime2 = moment(date2).format(format2);
document.getElementById("demo1").innerHTML = dateTime1;
document.getElementById("demo2").innerHTML = dateTime2;
<!DOCTYPE html>
<html>
<body>
<p id="demo1"></p>
<p id="demo2"></p>
<script src="https://momentjs.com/downloads/moment.js"></script>
</body>
</html>
Use different format or pattern to get the information from the date
var myDate = new Date("2015-06-17 14:24:36");
console.log(moment(myDate).format("YYYY-MM-DD HH:mm:ss"));
console.log("Date: "+moment(myDate).format("YYYY-MM-DD"));
console.log("Year: "+moment(myDate).format("YYYY"));
console.log("Month: "+moment(myDate).format("MM"));
console.log("Month: "+moment(myDate).format("MMMM"));
console.log("Day: "+moment(myDate).format("DD"));
console.log("Day: "+moment(myDate).format("dddd"));
console.log("Time: "+moment(myDate).format("HH:mm")); // Time in24 hour format
console.log("Time: "+moment(myDate).format("hh:mm A"));
<script src="https://momentjs.com/downloads/moment.js"></script>
For more info: https://momentjs.com/docs/#/parsing/string-format/
const currentDate = moment(new Date()).format('YYYY-MM-DD HH:mm:ss');
const Date = moment(dateTime).format('YYYY-MM-DD HH:mm:ss');

Formatting Date in Spreadsheet

I have a column in spreadsheet that is 'date'.
When I retrieve the date with the following code,
for (var i = 1; i < data.length; ++i) {
var row = data[i];
var date= row[0];
Logger.log(date);
I get
Wed Nov 12 2014 00:00:00 GMT+0800 (HKT)
Is there a way to just show the output as
Wed Oct 15 2014
I tried converting to JSON since it is an object, so that I can use substring
startDate = JSON.stringify(startDate);
startDate = startDate.substring(0, 14);
But it doesn't output correctly.
"2014-11-12T16:00:00.000Z"
Use formatDate() from the "Utilities" class:
var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'");
https://developers.google.com/apps-script/reference/utilities/utilities#formatDate(Date,String,String)
Did you try using "formatDate" in Utilities class
formatDate(date, timeZone, format)
try doing this:
var dateToFormat = row[0];
var formattedDate = Utilities.formatDate(new Date(dateToFormat), "GMT", "EEE, MMM d, yyyy");
Logger.log(formattedDate);

Resources