Categories
JavaScript Answers

How to Call the moment.calendar Method Without the Time in JavaScript?

Sometimes, we want to call moment.calendar method without the time in JavaScript.

In this article, we’ll look at how to call moment.calendar method without the time in JavaScript.

Call the moment.calendar Method Without the Time in JavaScript

To call moment.calendar method without the time in JavaScript, we can pass in null as the first argument.

For instance, we can write:

const cal = moment('2021-01-01')
  .calendar(null, {
    lastDay: '[Yesterday]',
    sameDay: '[Today]',
    nextDay: '[Tomorrow]',
    lastWeek: '[last] dddd',
    nextWeek: 'dddd',
    sameElse: 'L'
  })
console.log(cal)

We call moment with a date string.

Then we call calendar on the moment object with null and an object that has some date formatting options.

sameDay lets us specify the format for same day.

lastDay lets us specify the format for yesterday.

nextDay lets us specify the format for tomorrow.

lastWeek lets us specify the format for last week.

nextWeek lets us specify the text for next week.

sameElse specifies the format for all other dates.

Therefore, cal is '01/01/2021'.

Conclusion

To call moment.calendar method without the time in JavaScript, we can pass in null as the first argument.

Categories
JavaScript Answers

How to Get the Text Node After an Element with JavaScript?

Sometimes, we want to get the text node after an element with JavaScript.

In this article, we’ll look at how to get the text node after an element with JavaScript.

Get the Text Node After an Element with JavaScript

To get the text node after an element with JavaScript, we can use the nextSibling property to get the the text node after an element.

For instance, if we have:

<input type="checkbox" name='something' value='v1' /> hello world <br />

Then we write:

const text = document
  .querySelector('input[name="something"]')
  .nextSibling.nodeValue;
console.log(text)

We call document.querySelector with the selector string of the checkbox input to select it.

Then we get the value of the text node next to the checkbox with the nextSibling.nodeValue

Therefore, text is hello world according to the console log.

Conclusion

To get the text node after an element with JavaScript, we can use the nextSibling property to get the the text node after an element.

Categories
JavaScript Answers

How to Get the Hour Difference Between Two Times with Moment.js and JavaScript?

Sometimes, we want to get the hour difference between two times with moment.js and JavaScript.

In this article, we’ll look at how to get the hour difference between two times with moment.js and JavaScript.

Get the Hour Difference Between Two Times with Moment.js and JavaScript

To get the hour difference between two times with moment.js and JavaScript, we can use the duration, diff, asHours and asMinutes methods.

For instance, we can write:

const startTime = moment("12:26:59 am", "HH:mm:ss a");
const endTime = moment("06:12:07 pm", "HH:mm:ss a");
const duration = moment.duration(endTime.diff(startTime));
const hours = parseInt(duration.asHours());
const minutes = parseInt(duration.asMinutes()) % 60;

console.log(hours, minutes);

We parse 2 times into moment objects with the moment function.

We pass in the format of the time as the 2nd argument.

Then we call moment.duration with the difference between endTime and startTime that we get with the diff method.

Next, we get the hours part of the duration with the asHours method.

And we get the minutes part of the duration with the asMinutes method and get the remainder of that divided by 60.

Therefore, we get 17 45 from the console log.

Conclusion

To get the hour difference between two times with moment.js, we can use the duration, diff, asHours and asMinutes methods.

Categories
React Answers

How to validate input values with React?

To validate input values with React, we can use react-hook-form.

To install it, we run

npm i react-hook-form

Then we use it by writing

import React from "react";
import useForm from "react-hook-form";

function App() {
  const { register, handleSubmit, errors } = useForm();
  const onSubmit = (data) => {
    console.log(data);
  };

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input name="firstname" ref={register} />

      <input name="lastname" ref={register({ required: true })} />
      {errors.lastname && "Last name is required."}

      <input name="age" ref={register({ pattern: /\d+/ })} />
      {errors.age && "Please enter number for age."}

      <input type="submit" />
    </form>
  );
}

to call the useForm hook to return an object with a few properties we use.

Then we add a form element with the onSubmit prop set top handleSubmit(onSubmit).

We use handleSubmit to return a function that does form validation before calling onSubmit.

We call register to register form fields and add validation rules.

And we should errors using the errors property.

Categories
React Answers

How to fix div cannot appear as a descendant of p error with React?

To fix div cannot appear as a descendant of p error with React, we should make sure we don’t have divs that are inside p elements.

For instance, we shouldn’t write

<p>
  <div>...</div>
</p>

in our React components.

If we’re using the Material UI Typography component, we can change the component prop so that we don’t render a div inside a p element.

To do this, we write

<Typography component={"span"} variant={"body2"}>
  ...
</Typography>

to set the component prop to 'span' so that Typography doesn’t render a div inside a p element.