I want to use
.startOf('day')
on last saturday, and if today is saturday, I want to get the start of today. I tried this but is seems not working:
moment().isoWeekday("Saturday").startOf('day')
Try This
const today = moment();
const lastSat = today.isoWeekday(-1);
const startDayOfLastSat= lastSat.startOf('day')
Related
I am struggling to understand how to work with moment.js (UTC):
without utc
const now = moment().format() // result: 2022-06-04T02:48:50+03:00 (that's correct, but i need a format like this: 2022-06-04T02:48:50 ).
with utc
const now = moment.utc().format() // result: 2022-06-03T23:50:54Z // the problem is that as you can see the result is behind 3 hours
How to handle this situation?
From the API, I'm getting Trip booked Timezone(PST), Date-Time(2020-07-10 16:00:00). Now I want to check the 24 hours time difference with the user browser current time and display Cancel button otherwise, we need to hide the button. What I have done below is I'm passing trip booked time-2020-08-10 16:58:00 and booked timezone-PST which is getting from API then I'm finding the currentDate and taking the difference using the diff method. But I'm not getting current values. Any Help?
const departureDateWithTimeZone = moment.tz('2020-08-10 16:58:00', 'PST');
const currentDate = moment().format();
const diff = departureDateWithTimeZone.diff(currentDate, 'h');
Docs state that the last argument in moment.tz should be a time zone identifier.
Use America/Los_Angeles (or any other correct tz identifier) instead of PST:
const departureDateWithTimeZone = moment.tz('2020-08-10 16:58:00', 'America/Los_Angeles');
const currentDate = moment();
const diff = departureDateWithTimeZone.diff(currentDate, 'h');
console.log(`Current date: ${currentDate.format('YYYY-MM-DD HH:mm:ss Z')}`);
console.log(`Departure date: ${departureDateWithTimeZone.format('YYYY-MM-DD HH:mm:ss Z')}`);
console.log(`Diff in hours: ${diff}`);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.31/moment-timezone-with-data.min.js"></script>
I'm using fullcalendar with agendaWeek as defaultView. How can i make past days as non selectable .
For ex today is 15th August and i don't want to let user to select dates older , like on 13th August.
Thank you
It seems that the best solution is to use the option validRange
validRange: {
start: today,
end: '2025-06-01'
},
where
var today = new Date();
end the end date is some date in distant future.
I have a date like "Thu Sep 01 2016 00:00:00 GMT+0530 (IST)" which I need to send to server as ISO-8601 utc time. I tried like :
moment(mydate).toISOString()
moment.utc(mydate).toISOString()
moment(mydate).utcOffset("+00:00").toISOString()
but I am getting the result like
2016-08-31T18:30:00.000Z
which is 1day behind my intended time. So what can I do to make moment ignore my local timezone and see it as UTC?
Edit:
The expected output is
2016-09-01T18:30:00.000Z
And no, the initial input isn't a string rather a javascript "new Date()" value.
Reason this happens:
This happens because .toISOString() returns a timestamp in UTC, even if the moment in question is in local mode. This is done to provide consistency with the specification for native JavaScript Date .toISOString()
Solution:
Use the same function and pass true value to it. This will prevent UTC Conversion.
moment(date).toISOString(true)
const date = new Date("2020-12-17T03:24:00");
const dateISOStringUTC = moment(date).toISOString();
const dateISOString = moment(date).toISOString(true);
console.log("Converted to UTC:" + dateISOStringUTC)
console.log("Actual Date value:" + dateISOString)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
I take the same problem today and find the solution.
Here is the solution: moment(date,moment.ISO_8601)
var date = new Date();
console.log("Original Date");
console.log(date);
console.log("After Moment Format");
console.log(moment(date,moment.ISO_8601));
Test Execution:
Moment Documentation: MomentJs
how to get previous month first day and future month last day using moment.js,Please guide me with the relevant script required to achieve this
try something like
var prevMonthFirstDay = new moment().subtract(1, 'months').date(1)
and
var nextMonthLastDay = new moment().add(2, 'months').date(0)
or more verbose:
prevMonthFirstDay : moment().subtract(1, 'months').startOf('month')
nextMonthLastDay: moment().add(1, 'months').endOf('month')