Javascript Determine 10 days before specific date [duplicate] - javascript

This question already has answers here:
How to add days to Date?
(56 answers)
Closed 4 years ago.
So I am currently fetching some records from my database, and one of the records is a 'last edited' which is a date. However, I just want to display this 10 days before it has been exactly one year since the specific date. How would I approach this? I know how to deal with arrays and the fetching itself, but I'm not sure how I would deal with the date?
This is what I've done so far:
var startDate = fetchedDate;
var validDate = new Date(startDate);
var fullYear = validDate.getFullYear();
validDate.setFullYear(fullYear + 1);
And then I need to create a new date to compare it, I suppose? But how? I also want to know how many days it is until it has been one year.

If you wanna make it with pure javascript you can
var startDate = fetchedDate;
var validDate = new Date(startDate);
validDate.setYear(validDate.getFullYear() - 1);
validDate.setDate(validDate.getDate() - 10);
But I also will recommend you to use moment.js, is a very good javascript library to handle dates, otherwise sometimes is like trying to reinvent the wheel.

Related

Getting the date of the nth day after a given date [duplicate]

This question already has answers here:
Incrementing a date in JavaScript
(19 answers)
How to add days to Date?
(56 answers)
Closed 2 years ago.
I am looking to find the date for, say, the 100th day after any given day. Is there a way to do that via javascript?
I was hoping it could be as simple as below, but it isn't quite that simple.
var givenDay = new Date(01/01/2020);
var hundredthDay = new Date(givenDay + 100);
console.log(hundredthDay)
You can try using .setDate() and .getDate() combination.
The setDate() method sets the day of the Date object relative to the beginning of the currently set month.
The getDate() method returns the day of the month for the specified date according to local time.
Adding the required days to .getDate() as the following:
const givenDay = new Date('01/01/2020');
console.log(givenDay);
const result = new Date(givenDay.setDate(givenDay.getDate() + 1 + 100));
console.log(result);
I hope this helps!

How can I calculate the differences between now date and constant date. And edit it like 1d,1m,1w [duplicate]

This question already has answers here:
How to format time since xxx e.g. “4 minutes ago” similar to Stack Exchange sites
(40 answers)
Closed 3 years ago.
I want to show the transaction history on my project. But I want to show like transaction was 2m ago or 2h ago. For this reason how can I calculate and control this date calculation.
The variable came like
dd/mm/yyy hh:mm:ss
I want to show like after calculation
1m
1d
I would recommend moment.js to you moment.js docs - difference
const date1 = moment('2016-10-08 10:29:23');
const date2 = moment('2016-10-08 11:06:55');
const diff = date2.diff(date1);
const minutesDif= date2.diff(date1, 'minutes');
for your exact application i would recommend a look into the documentation

calculate whether weekend weekday or holiday [duplicate]

This question already has answers here:
Determine if a date is a Saturday or a Sunday using JavaScript
(6 answers)
Checking if date belongs to array of dates
(3 answers)
How to determine if date is weekend in JavaScript
(11 answers)
Moment JS Excluding Holidays
(2 answers)
Closed 3 years ago.
I have a start date and end date from my request need to find whether the given day is weekday or weekend or holiday(from list of holidays in db)
I tried using moment.js
moment.js is just a library to manage dates, what you need is an algorithm to find if one of the dates is weekend or weekday.
So what you can do is get the day number from the date (0-6) where 0 is sunday.
Let's say
var date = moment("2015-07-02");
var dow = date.day();
And about holidays or not or not, that's simple also, you need to get an array with all the dates in the same format that you are using with moment.js and iterate through the array to do it something like:
var holidays = ["2019-07-02", "2019-07-04"]
let isHoliday = holidays.find(x => x === "2015-07-02")
You need to do some work after all you need is here.
To determine if a given date is a weekend is actually pretty simple and already has an answer here.
To determine if a given date is holiday is bit more complex, you can not achieve it with native JavaScript, you will need the help of external package.
I recommend you to use moment-holiday, it has a simplest API:
moment('2017-12-25').isHoliday();
//Christmas Day
moment('2005-03-15').isHoliday();
//false
moment('2009-10-31').isHoliday('Halloween');
//true
moment('2017-12-31').isHoliday();
//New Year's Eve
moment('2017-12-31').isHoliday(null, true);
//false
Or you can use date-holidays.

Calculating age in Javascript with YYYY-mm-dd format [duplicate]

This question already has answers here:
Calculate age given the birth date in the format YYYYMMDD
(45 answers)
Closed 6 years ago.
I have birthday in following format:
1982-09-20
I need to get the exact age of the person from that birthdate ( compared with the current date).
What's the easiest way to do this in JS? Can someone help me out please?
Thanks!
I assume you want the exact age in years. If you want another value, it's all in that magic number of have in the function below, a bit verbose for clarity:
var MILLISECONDS_IN_A_YEAR = 1000*60*60*24*365;
function get_age(time){
var date_array = time.split('-')
var years_elapsed = (new Date() - new Date(date_array[0],date_array[1],date_array[2]))/(MILLISECONDS_IN_A_YEAR);
return years_elapsed; }
You would call:
get_age ('1982-09-20')
This is probably going to be marked as a duplicate and deleted.
There might be a better in built way, but this should work fine for your needs.

Get timestamp of current hour in Javascript [duplicate]

This question already has answers here:
How do I get a timestamp in JavaScript?
(43 answers)
Closed 7 years ago.
Is it possible for me to get the UTC time of current hour in Javascript?
I've tried the following:
var mins = new Date().getMinutes();
var hours = new Date().getHours();
var hourStamp = new Date().setHours(hours, 0,0,0);
var dates = new Date(hourStamp);
Here dates gives me the Unix Timestamp of the current hour. Is there a better/faster way to do this?
EDIT:
For example: I want the Timestamp for the current hour. So if it's 00:16 am on 12/04/2015, I want the timestamp of 12/04/2015 00:00. And, my method works. My question pertains to if there's a better method of doing the same.
var UTCHour = new Date().getUTCHours();
Like this?
The one works well:
var UTCTime= (new Date()).toUTCString();

Categories