News Articles momentjs

Mock time in Jest to check conditional rendering for React components

In my code, I use conditional logic to render 2 different components based on working/unworking hours. I need to compare it with the New York timezone – so I use currentTime = moment().tz(‘America/New_York’).hours() I face a problem with testing it with Jest or Enzyme, I try to mock time with all possible ways and it […]

How to set moment to empty depending on select

i want to set the start date and end date to undefined when select item change , i have use useEffect to handle this but on the UI the date don’t set to empty . this my code const [endDate, setEndDate] = useState<moment.Moment>(); const onChange = useCallback( (value) => { setEndDate(value); }, [setEndDate, setIsMoreThenBalance] ); […]

transaction timestamp for React.js app that display name and value in columns

I am very new to react and I am designing a 3 page app that gathers choices and values from page 1 and 2 and shows total choices on page 3. I have complete the items chosen and the values but I would like to add a timestamp when you get to page 3 with […]

Converting a date time to UTC but keeping the same date and time components

New to JavaScript dates and format and also using moment.js but I have the following date/time: "2021-08-21 00:00:00" but need to convert it to be this same date/time using moment.js, but in this format: "2021-08-21T00:00:00.000Z" To achieve the top date, I am using the following code: let value = "00:00:00"; let cdt = moment(value, ‘HH:mm:ss’); […]

Moment.js change the midnight time reset

Here is my current moment.js setup, and it works great! However, the endDate changes over at midnight (it moves up one day). I am sure that is how it is supposed to work and it makes sense, but I need it to change over at say, 8 AM. I have done some searching to no […]

Moment js not converting properly

So I’m trying to convert function with moment js and it still can’t be converted. This is declaration in html: <p class="text-muted small mb-0"> {{comments.postedAt | moment}} </p> Filters: filters: { moment: function(postedAt) { return moment(postedAt).fromNow(); } This is the part when I try to send it to database: let comm = { newComment: […]

Moment.js, days difference calculation with UTC offset

I’ve scripted a page for a friend that calculates days since a set date for a game he’s playing, but I’m getting confused when trying to calculate the date difference. This is my code so far // Get the day difference const getDayDifference = function() { // Get current time in offset const _start = […]

Calculate time remaining before a booking ends with Momentjs in React Native

I have an array of data I’m mapping through where a user selects certain time slots to book meeting rooms and am trying to take the end_date of this booking and work out how many hours/minutes are remaining before this booking ends. I am trying to do this using Momentjs and wondering what the best […]

Javascript Object key sort using moment.js

I have the following object structure { clientname: {Apr-18 : { … }, {Jan-18 : { … }, {Feb-18 : { … }, … } I am trying to sort the month (MMM-YY) keys so that it shows as follows { clientname: {Jan-18 : { … }, {Feb-18 : { … }, {Apr-18 : { […]

Moment JS Wrong Date

let time = moment(); console.log( time .year(2021) .week(34) .day(‘monday’) .format(‘D MMM YYYY’), ); <script src=”” integrity=”sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==” crossorigin=”anonymous” referrerpolicy=”no-referrer”></script> Displays 16 Aug 2021 but it should be 23 Aug 2021 – why? Works well if the year is 2020, 2019, etc but fails on year 2021 Source: JavaSript – Stack Overflow

moment.js: Return the first and last day of each month within an interval

I am trying to return an array that would give me the first and last day of each month, taking into account the start and end dates for the first and last month of the interval. I can’t seem to get get my head around it. I’m only able to do separate bits :'( Any […]

How to get the time from one moment and append in other

How to set the time in below case – In Moment js, The way I am setting the day .day(day), I wanted to update time from my futureDate to today date, How I can achieve that in moment. I am taking the time from this timeOnly timeOnly = moment(futureDate).format("hh:mm:ss") In result, now I am taking […]

How can i get utc time in Java Script, which is not dependent on client system?

In moment.js moment().utc() moment() In this both case i get Client side time. I have a problem when users change their time. For example if users clock is 2 hour ahead or behind. I am trying to create a timer in JS which should not be dependent on client side time. Any solutions? Source: JavaSript […]

How to restrict users to enter the date manually in the date field and user can enter date only in the given date range

I am working on a vueJs application in which I want to restrict user to manually enter the date out of range in the type=date field on the HTML page. I want to make that user can enter a date only in the given date range. `document .getElementsByName("col_date")[0] .setAttribute("min", moment().subtract(10,’day’).format("YYYY-MM-DD")); document .getElementsByName("col_date")[0] .setAttribute("max", moment().subtract(0,’day’).format("YYYY-MM-DD"))` enter […]

Display the word "Today" instead of data format in Datatable.js table

I would like to display the word "Today" instead of, for example, "Mon 16 Aug 2021" in my Datatable.js table. See fiddle here: I would like the result to display "Today" wherever today’s date is in the table: I am using moment.js to handle the date formatting but I cannot get the locale feature […]

How to get all particular weekDays between two date objects in momentJS

How can I get all weekDays(a particular weekday concerned) between a start Date and an end date inclusively. The function would take startDate, endDate (both as moment objects), dayNum(as integer)(0 – Sun, 1 – Mon … 6 – Sat) and return a list of moment objects of such days. I have written a function but […]

Conflict between DataTables SearchBuilder and Moment.js

I have just started using the SearchBuilder. Before that, I was using Moment.js ( and DateTime Moment ( When using the Searchbuilder for dates, I am getting the following error: Uncaught TypeError: t is not a function. Is any solution available? My code: $.fn.dataTable.moment(‘DD.MM.YYYY, HH:mm’); var table = $(‘#’ +‘#operatorLogs’).DataTable({ responsive: true, fixedHeader: false, […]

MomentJS format relative date from MongoDB

I’m trying to display a date as relative date using momentJS Currently I’m using express-generator, handlebars as view engine and mongoose to communicate with MongoDB The data that i get from mongoose is something like : 2021-08-15T13:44:02.212Z which is an ISO date i guess? but when i render it in handlebars it become something like […]

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.