This question already has answers here:
Moment.js Check a date is today
(5 answers)
Closed 6 years ago.
I am using moment().diff() to check if the provided date is today or not. I want to highlight today in my calendar using the code below.
isToday = moment().diff(moment('30 December, 2016','DDMMMMY'), 'days') === 0;
but the problem is it highlights 2 dates today and tomorrow.
Below is the image where moment diff returns 0 for two dates.
Is there anyway to address this or some other way to check for today.
Thanks in advance
Try this to check whether day is today.
var isToday = moment().format("DDMMYYYY") == moment('30 December, 2016','DDMMMMY').format("DDMMYYYY");
Since diff gives difference in seconds. You need further calculation from seconds to find today.
Related
This question already has answers here:
Get week of the month
(17 answers)
Closed 2 years ago.
I need to display a whole week from Monday To Sunday in following format "Week 2: 05.01 - 11.01" as an example
let d = new Date();
let date = d.getDate();
let day = d.getDay();
let currentWeek = Math.ceil((date + 6 - day) / 7);
this is what i have for the current week, i know that it will have some issues if the previous month is in the first week of the current month.
Any help as to how to make it accurate and reliable?
I need it to work in React Native!
I like using moment to deal with Dates, since date operations are somewhat complex considering month changes, leap years and all that.
Using moment, you'd just need to do
const today = moment();
const begginingOfCurrentWeek = today.startOf('week');
const endOfWeek = today.endOf('week);
I havent tested it, but it should be something along those lines. You can also format dates easily using moment().format('DD/MM/YY'), for example.
Here's the link to their documentation:
https://momentjs.com/
This question already has answers here:
getMonth in javascript gives previous month
(6 answers)
Date.getDay() javascript returns wrong day
(8 answers)
How do I format a date in JavaScript?
(68 answers)
Closed 2 years ago.
So I just started programming (I don't know if this is a correct word for it). I started making a website and wanted to add a date and clock. I followed a YouTube tutorial and it works but the month number is one month behind. Is there anyway to fix this problem? Also I wanted to make month number double if its number is less than 10. I wrote the JavaScript code and it messed with the month name (In this case Gegužė).
The getMonth() method returns the month in the specified date according to local time, as a zero-based value (where zero indicates the first month of the year).
const moonLanding = new Date('July 20, 69 00:20:18');
console.log(moonLanding.getMonth()); // (January gives 0)
// expected output: 6
This question already has answers here:
JavaScript function to add X months to a date
(24 answers)
Adding months to a Date in JavaScript [duplicate]
(2 answers)
Closed 2 years ago.
I'm trying to find a method that reliably subtracts 1 month from a javascript date object.
I have this code:
var shippedDate = new Date('12/31/2020');
var tempDate = new Date(shippedDate.setMonth(shippedDate.getMonth() - 1)); //subtract 1 month
alert(tempDate);
The value in tempDate after this code runs is 12/1/2020 when it should actually be 11/30/2020.
I checked my math with this online date calculator: https://www.timeanddate.com/date/dateadded.html?m1=12&d1=31&y1=2020&type=sub&ay=&am=1&aw=&ad=&rec=
Thanks.
December has 31 days so when you subtract 1 month, you get 31 November which doesn't exist, so it rolls over to 1 December.
You can test the date (day in month) to see if it's the same, and if not, set the date to 0 so it goes to the last day of the previous month.
Also, setDate modifies the Date object so no need to create a new one:
function subtractMonth(date, months) {
let d = date.getDate();
date.setMonth(date.getMonth() - months);
if (date.getDate() != d) {
date.setDate(0);
}
return date;
}
let d = new Date(2020, 11, 31); // 31 Dec 2020
console.log(subtractMonth(d, 1).toString()); // 30 Nov 2020
This has side effects so that sequentially subtracting 2 months may give a different result to subtracting 2 months in one go.
Also in regard to new Date('12/31/2020'), see see Why does Date.parse give incorrect results?
PS
I answered this before I remembered that there were plenty of questions about adding months that also cover subtracting. So I marked this question as a duplicate and rather than delete this answer, left it for posterity.
If you wish to vote for an answer, please go to one of the duplicates and vote for an answer there. :-)
On my own experience, I may qualify all around Date calculation in javascript as completely unbearable pain.
Avoid as possible own crafted function to any Date manipulation. There are too many traits to lose mind at all. Timezones, wrong clocks, timezone on your own host vs. timezone on server, unexpected toString conversion according to local host timezone/clock.
If you rally need to make some dates calculation use battle tested library, like date-fns, moment.js, etc.
By the way your example almost correct, you just have chosen not suitable time to try to test it. The only one that I see problematic it's using setMonth that mutate original shippedDate.
This question already has answers here:
Javascript code for showing yesterday's date and todays date
(6 answers)
Closed 6 years ago.
I want to plot a graph by fetching the previous seven days using the present day in Javascript.
Which should pass through the tests of Leap year, days of mont(30/31), Year change.
Thank you in advance.
var d = new Date();
var yesterday = d.setDate(d.getDate() - 1);
Simply replace - 1 with - 2, - 3 and so on for the previous seven days.
This question already has answers here:
How do I get the current date in JavaScript?
(61 answers)
Closed 8 years ago.
I wanted to know how can I get todays date in as July 17,2014 format. I am new to javascript..
please help
but
I have tried using var today = new Date();..but it gives me current date and not in the format I want.
Try to make use of following method of Date in javascript:
getFullYear(); // 2011
getMonth(); // 0-11, 0-based month in year,
getDate(); // 0-31, 1-based day of month,