Categories
JavaScript Answers

How to convert from UTC to desired time zone with MomentJS and JavaScript?

To convert from UTC to desired time zone with MomentJS and JavaScript, we can use the moment-timezone library.

To use it, we write:

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.34/moment-timezone.min.js" ></script>

to add the moment.js and moment-timezone scripts.

Then we write:

moment.tz.add([
  'America/New_York|EST EDT|50 40|0101|1Lz50 1zb0 Op0'
]);

const d = new Date(2022, 5, 1);
const myTimezone = "America/New_York";
const myDatetimeFormat = "YYYY-MM-DD hh:mm:ss a z";
const myDatetimeString = moment(d).tz(myTimezone).format(myDatetimeFormat);
console.log(myDatetimeString)

to call moment.tz.add to add the time zone data for Eastern time.

Then we create a date d with the Date constructor.

Next, we call moment with d to create a moment object.

Then we call tz with myTimezone to convert the time to the given time zone.

Next, we call format to return a date string with the format specified by myDatetimeFormat.

Therefore, myDatetimeString is '2022-06-01 03:00:00 am EDT'.

Categories
JavaScript Answers

How to dynamically insert an iframe in a div with JavaScript?

Sometimes, we want to dynamically insert an iframe in a div with JavaScript.

In this article, we’ll look at how to dynamically insert an iframe in a div with JavaScript.

How to dynamically insert an iframe in a div with JavaScript?

To dynamically insert an iframe in a div with JavaScript, we can use createElement and appendChild.

For instance, we write:

<div>

</div>

to add a div.

then we write:

const div = document.querySelector('div')
const iframe = document.createElement('iframe')
iframe.src = 'https://example.com'
div.appendChild(iframe)

to select the div with querySelector.

Then we call createElement to create an iframe.

Next, we set the iframe’s src attribute by setting the src property.

Finally, we call div.appendChild with iframe to append the iframe as the child of the div.

Conclusion

To dynamically insert an iframe in a div with JavaScript, we can use createElement and appendChild.

Categories
JavaScript Answers

How to chain promises with promises inside then() with JavaScript?

Sometimes, we want to chain promises with promises inside then() with JavaScript.

In this article, we’ll look at how to chain promises with promises inside then() with JavaScript.

How to chain promises with promises inside then() with JavaScript?

To chain promises with promises inside then() with JavaScript, we can return a promise in the then callback.

For instance, we write:

Promise
  .resolve(1)
  .then((res) => {
    console.log(res)
    return Promise.resolve(2)
  })
  .then((res) => {
    console.log(res)
    return Promise.resolve(3)
  })
  .then((res) => {
    console.log(res)
  })
  .catch((e) => {
    console.error(e);
  });

to call then with callbacks that returns another promise.

Promise.resolve returns a promise.

res has the resolved value of the previous promise.

Each then callback will run in the same order they’re listed.

Therefore, we see

1
2
3

logged.

Conclusion

To chain promises with promises inside then() with JavaScript, we can return a promise in the then callback.

Categories
JavaScript Answers

How to disable an input field using JavaScript?

Sometimes, we want to disable an input field using JavaScript.

In this article, we’ll look at how to disable an input field using JavaScript.

How to disable an input field using JavaScript?

To disable an input field using JavaScript, we can set the disabled property of the input to true.

For instance, we write:

<input>

to add an input.

Then we write:

const input = document.querySelector('input')
input.disabled = true

to select an input with querySelector.

And then we set input.disabled to true to disable the input.

Conclusion

To disable an input field using JavaScript, we can set the disabled property of the input to true.

Categories
JavaScript Answers

How to delete cookie in Express and JavaScript?

Sometimes, we want to delete cookie in Express and JavaScript.

In this article, we’ll look at how to delete cookie in Express and JavaScript.

How to delete cookie in Express and JavaScript?

To delete cookie in Express and JavaScript, we can use the res.clearCookie method.

For instance, we write:

const express = require('express')
const app = express()
const port = 3000

app.get('/logout', (req, res) => {
  res.clearCookie('cookie');
  return res.sendStatus(200);
})


app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

to call res.clearCookie with the key of the cookie to delete in the /logout endpoint.

Conclusion

To delete cookie in Express and JavaScript, we can use the res.clearCookie method.