JavaScript - Unable to manipulate New Date() to get different date values - javascript

I am very new to JavaScript and had a question. I have done research and I just can't figure out why the following function isn't working for me. I am trying to establish Current Date and then getting various values from that value such as Current Date minus one year, Current Date plus one year, etc.
For Current Date minus year I have the following but it's not working (I would much prefer to have a one liner for other reasons:
var currentDateMinusOneYear = new Date(new Date().setFullYear(new Date().getFullYear() - 1));
console.log(currentDateMinusOneYear);
First we should get fullYear from a new Date, decrease by 1, set that as the year of a new Date. Then we wrap the whole thing in a Date constructor.
Console just returns an empty object. Not sure why. Please advise.

Related

Having trouble working with dates in Google Apps Script

I'm working with a number of dates in Google Sheets, but it seems to always enter the date as a string.
sheet.getRange(3,11,sheet.getLastRow()-3, 1).setValue(Utilities.formatDate(new Date(), "UTC-5", "dd/MM/yyyy"));
is how I enter the date, but when I test it with
Logger.log(typeof extractedDate); it tells me that its a string.
If I'm working with hundreds of dates, it seems to me that it would be a huge waste of time/resources to have to parse each date as a string each time and convert it.
Instead of
sheet.getRange(3,11,sheet.getLastRow()-3, 1).setValue(Utilities.formatDate(new Date(), "UTC-5", "dd/MM/yyyy"));
use
sheet.getRange(3,11,sheet.getLastRow()-3, 1).setValue(new Date());
The above will pase the current date and time to the spreadsheet which will be displayed accordingly to the cell formatting.
If you want to pass the date without the time, instad of new Date() use
new Date(new Date().getFullYear(),new Date().getMonth() , new Date().getDate())
Better alternative
var now = new Date();
var today = now.setHours(0,0,0,0);
sheet.getRange(3,11,sheet.getLastRow()-3, 1).setValue(today);
Related
Set Date/Time to 00:00:00
How to get current date without time?
Javascript date method without time
Script inserted today's date into spreadsheet is wrong
create button to add new line with today date and blank other fields

How to get last day of a given year with moment?

I can hard-code the last day of a year because anyway it will always be 31st December. There are many solutions using date, js, jquery. But I'm working on an Angular project and hence my code is in typescript. My tech lead wants me to do this using moment. I'm not supposed to hard-code any date. Is there any built-in method provided by moment to fetch the last day of a given year. And I'm saying given year because in my case given year is dynamic. It can change anytime. I'm using endOf() method. I'm getting error with custom year. I mean:
const lastDayOfYear = moment().endOf('year') is working fine, but:
const lastDayOfYear = moment().endOf(2025) is giving me this error:
Argument of type '2025' is not assignable to parameter of type 'StartOf'
I also tried:
const lastDayOfYear = moment().endOf("2025");
year=2025; const lastDayOfYear = moment().endOf(year);
How will this method work? I've gone through the entire Moment docs. Or should I stick to Date of javascript. Something like this:
new Date(new Date().getFullYear(), 11, 31);
Please provide a solution.
PS: I want last date of a given year in MM-DD-YYYY format only.
Well if you really wanted to work that out with moment instead of using `12-31-${year}` you could instead use:
moment([year]).endOf('year').format('MM-DD-YYYY')
moment(date).endOf('year');
Where date is a Date somewhere in that year.
set current date in moment new Date()
const now = moment(new Date()).endOf('year');
alert(now);

How to create a Date object from year, month and date strings?

I'm currently just practising JavaScript and I'm trying to create a programme that calculates how many days there are until your next birthday.
I have seen on this site that there is a daysBetween function that I can use to tell the time difference between two dates (I just need to turn these dates into the millisecond value since the 1st Jan 1970).
The problem is that, although one of those dates is the current date (which is easy to convert into its millisecond value), the second is derived from answers the user inputs as a string into a prompt command (there are three different boxes that ask for the year, month and date of their birth). Is there a way I can convert these input strings into a date format that I can then use to find the days between today's date and their next birthday?
Thanks and sorry if this is a stupid question!
Remember in JS that months are indexed from 0, so January === 0.
var date = new Date('2017','5','25'); would be today, assume you have something like:
var userDd = '25';
var userMm = '6'; // User doesn't know the months are indexed at 0.
var userYy = '2017';
var date = new Date(userYy, userMm - 1, userYy);
date.getTime(); // returns the milliseconds value.

Using javascript to perform some date calculations/comparisons

I have a Date object and I'm trying to add a year to the today's date. I also have to have a way to compare the date that's in the date object (newly made + one year) and today's date. How do I compare today's date with the date in the variable? The point is, I need to have a way to know if today's date is the same or greater to the expiration date, that way I could redirect users to a different location... thanks all!
here's my Date object
var expDate = new Date();
The basic JavaScript Date object has stuff built in to handle all of that natively.
For adding a year to your date simply do this:
var currentDate = new Date();
var futureDate = new Date(currentDate);
futureDate.setFullYear(futureDate.getFullYear() + 1);
That gets the current day as the date, and then creates a new date, based on today's date and sets the yea to the current year plus 1.
As for the comparison, simply use <, <=, >, >=, ==, and !=. JavaScript understands what to do with those operators when Date objects are involved and will compare the two dates appropriately.
On thing that I might suggest doing (since you only care about the actual date, and not the time element of the Date object, is add in this line of code after getting today's date:
currentDate.setHours(0, 0, 0, 0);
That will set the hour, minute, second, and millisecond values to 0, so that only the date is a factor when doing any comparisons.
For everything that you could ever need to know about the JavaScript Date object, check out here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Edit: Fixed syntax issue.

Javascript need two date instances Keep getting references instead

How to have a date and subtract 1 year and have a second date for use in display and caculations?
I'm a newbe with JS and find myself fighting the date object
I have declared both currentDate and purchaseDate As var new date() in the globals area
But now in a function I try to assign the Original date as "currentDate and the purchaseDate" as one year later,, The alert shows that I have changed the vaue of currentDate rather than just the value of purchaseDate as I intended..
Not what I want! So I get pass by reference vs by value but don't know how to get this so that I have two separate values for the currentDate and the purchaseDate (one year earlier)
currentDate = data.getValue(0,2);
purchaseDate = valueOf(data.getValue(0,2));
purchaseDate.setFullYear(purchaseDate.getFullYear()-1);
alert(purchaseDate);
so this code fails also; That is,, purchase date is 1 year back but so is current date
currentDate = data.getValue(0,2);
purchaseDate = data.getValue(0,2);
purchaseDate.setFullYear(purchaseDate.getFullYear()-1);
alert(purchaseDate);
The code which you posted is too ambiguous to reliably point the root cause of your problem (it's unclear what valueOf() is doing), but basically, you indeed need to create a new Date instance based on the time of the other Date. Here's how you could do this, assuming that currentDate is a real Date as well.
var purchaseDate = new Date(currentDate.getTime());
Here's a full kickoff example:
var currentDate = new Date();
var purchaseDate = new Date(currentDate.getTime());
purchaseDate.setFullYear(purchaseDate.getFullYear() - 1);
alert(currentDate); // Today.
alert(purchaseDate); // Today minus one year.

Categories