My code is this:
const nowDate = moment(new Date()).format("DD/MM/YYYY");
let paraRepDate = '01/01/2021';
let calcParaDate = '30/06/2021';
var x = moment(calcParaDate).isBefore(nowDate)
console.log(x) // false
How is it possible?
Default format of momentjs is MM/DD/YYYY. In your solution you can see warning information in console:
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/ for more info.
Solution
You have to specify format moment(date, format). Something like this:
const format = 'DD/MM/YYYY';
const nowDate = moment();
const calcParaDate = '30/06/2021';
const isBefore = moment(calcParaDate, format).isBefore(nowDate)
console.log(isBefore);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment-with-locales.min.js" integrity="sha512-LGXaggshOkD/at6PFNcp2V2unf9LzFq6LE+sChH7ceMTDP0g2kn6Vxwgg7wkPP7AAtX+lmPqPdxB47A0Nz0cMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
Also you don't have to pass new Date() in moment() for current datetime.
Related
in a project we are using momentjs with date. And from backend we become the date in the following format: 2016-10-19T08:00:00Z (don't ask me why...)
Now we are setting a new date in frontend from some selectboxes. And I am trying to convert this in the same format:
const date = '25.03.2021';
const hour = '13';
const minute = '45'; // this 3 values come from value of selectboxes
const rawDate = moment(date).hour(hour).minute(minute);
// trying to convert to 2021-03-25T13:45:00Z
rawDate.format(); // output: 2021-03-25T13:45:00+00:00
rawDate.format('DD.MM.YYYY hh:mm:ss'); // output: 03.01.2022 08:00:00
rawDate.format('DD.MM.YYYY hh:mm:ss z'); // output: 03.01.2022 08:00:00 UTC
rawDate.format('DD.MM.YYYY hh:mm:ss Z'); // output: 03.01.2022 08:00:00 +00:00
rawDate.toISOString(); // output: 2022-01-03T08:00:00.000Z
I know I could probably just use format() or toISOString() and slice/replace the last bit. But I like to know is there a way without any string concat/manipulation?
You could use moment.utc() to ensure your date is in UTC, then use .format() with the format string DD-MM-YYYYTHH:mm:ss[Z].
I'd also suggest explicity defining the format you are parsing from in the moment() call, e.g. pass 'DD.MM.YYYY' as the second argument.
The reason the backend takes dates in this format is that it's a standardized way of formatting dates to make them machine-readable and consistent (ISO 8601)
const date = '25.03.2021';
const hour = '13';
const minute = '45';
// Raw date will be in the UTC timezone.
const rawDate = moment(date, 'DD.MM.YYYY').hour(hour).minute(minute).utc();
console.log(rawDate.format('DD-MM-YYYYTHH:mm:ss[Z]'));
<script src="https://momentjs.com/downloads/moment.js"></script>
You can try convert to UTC ..?
i.e. Do you intend to make use of a UTC date/time..?
const date = '2021-03-25';
const hour = '13';
const minute = '45'; // this 3 values come from value of selectboxes
const rawDate = moment(date).hour(hour).minute(minute);
const utc = moment.utc(rawDate);
console.log(rawDate.format('DD.MM.YYYY hh:mm:ss'));
console.log(utc.format()); //2021-03-25T11:45:00Z
I've a string date in ISO format and a string in format HH:mm.
I want to know if the hour of the string ISO date is same or before the string in format HH:mm.
Example:
const isoDateString = '2021-09-28T07:30:00Z' // UTC
const hour = '07:30' // not UTC
-> result true
---
const isoDateString = '2021-09-28T07:30:00Z' // UTC
const hour = '08:30' // not UTC
-> result false
I'm using moment and this is my code:
const TIME_FORMAT = 'HH:mm'
const isoDateString = '2021-09-28T09:30:00Z'
const hour = '07:30'
const isHourSameOrBeforeIsoString = moment(
moment(isoDateString).format(TIME_FORMAT),
).isSameOrBefore(moment(hour, TIME_FORMAT));
console.log(isHourSameOrBeforeIsoString)
It doesn't work. It returns false in both cases. Why?
Use moment.utc() when constructing your iso date string because you should handle that as in UTC.
I also added TIME_FORMAT inside moment constructor of formatted iso date string.
const TIME_FORMAT = 'HH:mm'
const isoDateString = '2021-09-28T09:30:00Z'
const hour = '07:30'
const isHourSameOrBeforeIsoString = moment(
moment.utc(isoDateString).format(TIME_FORMAT), TIME_FORMAT
).isSameOrBefore(moment(hour, TIME_FORMAT));
console.log(isHourSameOrBeforeIsoString)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
Here is a more reasonable way to check in my opinion:
The logic you had was correct, but the reason it is returning false is because your isoDateString is returning 8:30 and the hour you are comparing it to is 7:30, like Krzysztof mentioned in their comment, it could be a time zone issue:
var format = 'hh:mm'
// var time = moment() gives you current time. no format required.
var time = moment('2021-09-28T09:30:00Z',format),
testTime = moment('07:30', format);
console.log(moment(time).format(format));
console.log(moment(testTime).format(format));
if (time.isSameOrBefore(testTime)) {
console.log('is before')
}
if(time.isSameOrAfter(testTime)){
console.log('is after')
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" referrerpolicy="no-referrer"></script>
I'm getting an error using moment-timezone with America/Sao_Paulo trying to parse date with America/Sao_Paulo offset. I've created this code:
const date = "2019-1-1 23:30";
const format = "YYYY-M-D HH:mm";
const timezone = "America/Sao_Paulo";
const dateMoment = moment.tz(date, format, timezone);
The output of dateMoment is 2019-01-01T23:30:00-02:00 but I expect 2019-01-01T23:30:00-03:00, since America/Sao_Paulo has offset -03:00.
Am I missing something or did I initialize dateMoment wrong?
const date = "2019-1-1 23:30";
const format = "YYYY-M-D HH:mm";
const timezone = "America/Sao_Paulo";
const dateMoment = moment.tz(date, format, timezone);
console.log(dateMoment.format());
console.log(moment.tz(timezone).format('Z'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js"></script>
<script type="text/javascript" src="http://momentjs.com/downloads/moment-timezone-with-data.js"></script>
On January 1st 2019, Sao Paulo was still on Summer Time. It was UTC-2 until February 17th.
Brazil abolished summer time going forward, but that hadn't taken effect yet.
I'm trying to convert the time ( time alone ) from a known timezone to my local timezone with Moment.js.
I wrote the following function and, I am getting invalidDate as the output.
const convertToLocalTime = (time, tz) => {
const t = moment.tz(time, tz)
const localTime = t.local()
}
time is just time; without any date eg: 10:06 am and,
tz is a timezone string for eg: Europe/Berlin
What am I doing wrong?
See Parsing in Zone:
The moment.tz constructor takes all the same arguments as the moment constructor, but uses the last argument as a time zone identifier.
Since your input (10:06 am) is not in ISO 8601/RFC 2822 recognized format (see moment(String) docs), you have to pass format parameter as shown in moment(String, String).
Here a live sample:
const convertToLocalTime = (time, tz) => {
const t = moment.tz(time, 'hh:mm a', tz)
const localTime = t.local()
return localTime;
}
const res = convertToLocalTime("10:06 am", 'Europe/Berlin');
console.log( res.format('hh:mm a') );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.14/moment-timezone-with-data-2012-2022.min.js"></script>
I am using moment.js and would like to show the user's local timezone name like CET or PST using
var timezone_local = moment.tz().zoneName();
document.getElementById('timezone_local').innerHTML = timezone_local;
Those lines do not work. Thanks for your help!
According to the official moment document, you can use moment-timezone
moment.tz.guess();
For further formatting, refer to this.
Edited :
var zone_name = moment.tz.guess();
var timezone = moment.tz(zone_name).zoneAbbr()
console.log(timezone);
Refer to this working fiddle.
I'm just doing this. The z in the format adds the proper abbreviation for the timezone.
moment().tz('America/New_York').format('MMMM Do YYYY, h:mm a z');
.format("z") with a lowercase z prints out just the timezone abbreviation.
Here are a few examples:
let currentTime = moment().tz(moment.tz.guess());
console.log(currentTime.format("z")); // PST
let parisTime = moment().tz("Europe/Paris");
console.log(parisTime.format("z")); // CET
let indiaTime = moment().tz("Asia/Kolkata");
console.log(indiaTime.format("z")); // IST
let newYorkTime = moment().tz("America/New_York");
console.log(newYorkTime.format("z")); // EST
let newYorkTimeDuringDST = moment("2020-08-01").tz("America/New_York");
console.log(newYorkTimeDuringDST.format("z")); // EDT
var timeZone = moment.tz.guess();
const zoneName = moment.tz(timeZone).zoneName();
return moment().tz("America/New_York").zoneName();
Include moment.js and moment-timezone-with-data.js javascript library.
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.11/moment-timezone-with-data.js"></script>
Then use the below code to get the browser timezone name.
<script>
var timezone = moment.tz.guess();
console.log(timezone);
</script>
See the timezone name in the browser console.