Get current week and date of week in react native [duplicate] - javascript

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/

Related

How could I validate/compare two dates in node js to ensure endDate is withing after 24hours of startDate? [duplicate]

This question already has answers here:
Node.js dates comparison
(3 answers)
Closed 4 years ago.
I have two dates i.e. startDate = 2018-11-14T08:55:03.021Z and endDate = 2018-11-14T09:13:03.097Z. How can I compare this two dates to ensure the endDate is within 24 hours after startDate. I want to add more validation like endDate should not be greater than currentDate. How could I do this? Is there any algorithm available?
It will be easier if you use some library to handle dates. I prefer using moment library for date related operations in JavaScript https://momentjs.com/docs/. You can use the code below to get the difference in hours using moment.
let momentStartDate = moment(startdate);
let momentEndDate = moment(endDate);
let duration = moment.duration(momentEndDate.diff(momentStartDate));
let differenceInHours = duration.asHours();
console.log(differenceInHours)

Javascript: Get epoch date as from predefined date is using todays date instead [duplicate]

This question already has answers here:
Convert UNIX to readable date in javascript
(3 answers)
Closed 4 years ago.
JSFiddle: https://jsfiddle.net/u8w3v9fd/1/
I am trying to get the day, month and year from a date that is passed form the database in the format: DD/MM/YYYY. However I can't even seem to get the correct date to show.
Here is my code:
var time = "1522843537";
var regDateOriginal = new Date(time);
var regDate = new Date();
regDate.getMonth(regDateOriginal);
regDate.getHours(regDateOriginal);
regDate.getDate(regDateOriginal);
document.write("<p style='color: #fff'>" + regDate.getDate(regDateOriginal) + "</p>");
As you can see, this is returning:
21
Which is todays date. It should be 4
I have googled it and hacked around with various versions for the past 45 mins. I am a junior and would really appreciated a nicely commented piece of code so I can learn instead of just copying and pasting.
Thank you for your help.
From here
var time = 1522843537;
var d = new Date(0); // The 0 there is the key, which sets the date to the epoch
d.setUTCSeconds(time);
console.log(d.getDate());
Of course, you can still do all the other Date functions as needed.

Comparing if the provided date is today using moment [duplicate]

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.

Get the previous date from today's date in javascript [duplicate]

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.

Current Date without Time, and Text with Option to go back upto a week [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 6 years ago.
I am not a very professional coder. I am just soo used to looking at them and I edit the very basics. First time into Javascript though. I have a website that i use yo code. It only allows to write css and html. Now i have created a comment section under that, also a Text called "Last Updated"
What I want is, I don't have to update the thread, but the Last Updated, automatically gets followed by today's date. Hence I have been searching for hours but failed to find. I need the format - Date Month Year [Example: 5 June 2016]
But heres the tough and challenging part. Also in the comments section I am trying to add dates that automatically update.So the top 2 latest comments are always on today's date, followed by the next 4-5 on yesterdays date, and the earlier 4-5 on the day before yesterday. Since the comments are also added by me, I need to keep those comments look recently added. The format would be same. Date Month Year. If anyone could help me.
To get the current date and add to it in the desired format:
var current_date = new Date();
var month = current_date.getMonth() + 1;
var day = current_date.getDate();
var year = current_date.getFullYear();
// Add 4 days to the current day.
day += 4;
var formatted_date_string = day + '/' + month + '/' + year;
console.log(formatted_date_string);

Categories

Resources