How to check if date is a current date with using dayjs? - javascript

I have a problem with checking if my date in unix is a current date with using dayjs library, I try like below:
const date = 1631978008; //today: 2021-09-18
const isToday = dayjs().isSame(date, 'day'); //return false
but always return me false when my date is a today date, can someone tell me why? it should return true :/
thanks for any help!

You can use the unix function to parse the unix timestamp in seconds before comparing it with the current time.
const date = 1631978008; //today: 2021-09-18
const isToday = dayjs().isSame(dayjs.unix(date), 'day');
console.log(isToday);
<script src="https://unpkg.com/dayjs#1.8.21/dayjs.min.js"></script>

Related

Comparing date and time with moment js

In my react app I am receiving an endDate, and also separately receiving an endTime. I have to check if that endDate and endTime are before the current date and time.
let endDate = "04/13/2022";
let endTime = "22:00"
console.log(moment(endDate, "MM/DD/YYYY", endTime).isBefore(moment());
//true
Today's date is the same as the endDate but the time is earlier than the endTime so I should see False instead of true. The times are not being compared. Does anyone know how to resolve this?
Combine the date and time strings, and parse as one full date-time string.
const endDate = '04/13/2022';
const endTime = '22:00';
const date = moment(`${endDate} ${endTime}`, 'MM/DD/YYYY HH:mm');
console.log(date.isBefore(moment())); // false
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.2/moment.min.js"></script>

date comparison moment js

I want to compare two dates and my date format is DD-MM-YYYY but I don't know why my output returns false when I compare my dates.
example 1
const date1 = '30-06-2021';
const date2 = '10-01-2022';
const result = moment(date1) < moment(date2); // return false, should return true
example 2
const date1 = '30-06-2021';
const date2 = '10-01-2022';
const result = moment(date1).isBefore(date2); // return false, should return true
There are two issues there:
You're expecting moment to guess the format of your dates, but it can't do that reliably. Always provide a format string if your string isn't in a RFC2822 or ISO-8601 format. moment itself warns you about doing that in the dev version of the library:
Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged. Please refer to http://momentjs.com/guides/#/warnings/js-date/
< cannot be used to meaningfully compare objects. If you want to know if a date is before another date, use the isBefore method.
For example:
const date1 = "30-06-2021";
const date2 = "10-01-2022";
const format = "DD-MM-YYYY";
const result = moment(date1, format).isBefore(moment(date2, format));
const date1 = "30-06-2021";
const date2 = "10-01-2022";
const format = "DD-MM-YYYY";
const result = moment(date1, format).isBefore(moment(date2, format));
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

Days between dates in DayJS UK Format

Im using dayjs, and I'm trying to calculate the days between 2 days so that I can output Today, Tomorrow and X days time
The problem I'm having though seems to be with the date formats
const date1 = dayjs('31/10/2021');
const date2 = dayjs().format("DD/MM/YYYY")
let hours = date2.diff(date1, 'days')
No matter how I try, I'm getting date2.diff is not a function when I add .format to date2. Does anyone know how to format this correctly and get the correct outcome?
If i try const date1 = dayjs('31/10/2021').format("DD/MM/YYYY"); then it errors with invalid date
You can try the following:
const date1 = dayjs('2021-01-25');
const date2 = '01/11/2020'; // or '2020-11-01'
let hours = date1.diff(date2, 'day')
console.log(hours) // 380
The format function just returns a string. This is why date2.diff is not a function.
The diff function is only available for dayjs objects (not for strings).
Docs for difference function: https://day.js.org/docs/en/display/difference
Docs for format function: https://day.js.org/docs/en/display/format
const date1 = dayjs('2019-01-25')
date1.diff('2018-06-05', 'day', true)
You don't need to call format function, just use dayjs constructor to convert both dates to dayjs object and return the difference between the 2 dates in number of days.

How to convert the DD-MM-YYYY HH:mm format to epoch time using moment or js?

I have a date time picker and it omits date in format of a DD-MM-YYYY HH:mm a format. I want to convert it into epoch time to store it in the database backend.
I have tried using the following methods so far, but all of them return NaN as output:
var loadingPlannedUnTime = this.state.loadingPlannedUnTime;
console.log("normal conversion"+loadingPlannedUnTime);
//returns 9-08-2020 15:08:00
//Moment method
var loadingPlannedUnTime = moment(this.state.loadingPlannedUnTime).unix();
console.log("moemnt"+loadingPlannedUnTime)
//Returns NaN
//JS getTime() method
var pickupDateTime = new Date(this.state.loadingPickupTime);
var loadingPickupTime = pickupDateTime.getTime();
console.log("new date and gettime"+loadingPickupTime)
//Returns NaN
What is the correct method to convert it to epoch time?
https://momentjs.com/docs/
If you know the format of an input string, you can use that to parse a moment.
moment("12-25-1995", "MM-DD-YYYY");
const x = moment('24-12-2019 09:15', "DD-MM-YYYY hh:mm");
console.log(x.format())
<script src="https://rawgit.com/moment/moment/2.2.1/min/moment.min.js"></script>
const matches = loadingPlannedUnTime.match(/(\d{1,2})-(\d{1,2})-(\d{2,4}) (\d{1,2}):(\d{1,2}):(\d{1,2})/);
if (!!matches) {
// new Date(year, month, day, hours, minutes, seconds, milliseconds)
const epoch = new Date(matches[3], matches[2] - 1, matches[1], matches[4], matches[5], matches[6]).getTime();
console.log(epoch);
}

Get timestamp from date string without timezone and timezone name apart

I have a dateTime string in this (bad, I know) format coming from an external API:
const startDate = '2/13/2020 15:00';
and the timezone name:
const timezoneName = 'America/New_York';
Which means that the dateTime is 2/13/2020 15:00 in New York.
Any idea for an elegant way to get timestamp (or JavaScript date object)?
I don't mind using moment.js or/and moment-timezone if it helps.
Moment and Moment-Timezone are for legacy code.
For new applications, the Moment team recommends Luxon.
const startDate = '2/13/2020 15:00';
const timezoneName = 'America/New_York';
const dateTime = luxon.DateTime.fromFormat(startDate, 'M/d/yyyy HH:mm',
{ zone: timezoneName });
const utcDateTime = dateTime.toUTC();
const s = utcDateTime.toISO();
console.log(s); //=> "2020-02-13T20:00:00.000Z"
Using moment-timezone should work:
const moment = require('moment-timezone');
const t = moment.tz("2/13/2020 15:00", "MM/DD/YYYY HH:mm","America/New_York");
console.log(t.toISOString()); // Prints '2020-02-13T20:00:00.000Z'

Categories

Resources