Categories
JavaScript Answers

How to remove seconds/ milliseconds from Date convert to ISO string with JavaScript?

Sometimes, we want to remove seconds/ milliseconds from Date convert to ISO string with JavaScript.

In this article, we’ll look at how to remove seconds/ milliseconds from Date convert to ISO string with JavaScript.

How to remove seconds/ milliseconds from Date convert to ISO string with JavaScript?

To remove seconds/ milliseconds from Date convert to ISO string with JavaScript, we use the setSeconds method.

For instance, we write

const d = new Date();
d.setSeconds(0, 0);
console.log(d.toISOString());

to call setSeconds on date d to set its seconds and milliseconds to 0.

And then we call toISOString to return the ISO string from the date.

Conclusion

To remove seconds/ milliseconds from Date convert to ISO string with JavaScript, we use the setSeconds method.

Categories
JavaScript Answers

How to replace comma with a dot in the number with JavaScript?

Sometimes, we want to replace comma with a dot in the number with JavaScript.

In this article, we’ll look at how to replace comma with a dot in the number with JavaScript.

How to replace comma with a dot in the number with JavaScript?

To replace comma with a dot in the number with JavaScript, we use the replace method.

For instance, we write

const newStr = str.replace(/,/g, ".");

to call replace on string str to replace all commas with dots and return the new string.

We use /,/ to match a comma.

The g flag makes replace replace all matches.

Conclusion

To replace comma with a dot in the number with JavaScript, we use the replace method.

Categories
Angular Answers

How to print HTML template in Angular and TypeScript?

Sometimes, we want to print HTML template in Angular and TypeScript.

In this article, we’ll look at how to print HTML template in Angular and TypeScript.

How to print HTML template in Angular and TypeScript?

To print HTML template in Angular and TypeScript, we call the window.print method.

For instance, we write

<div class="doNotPrint">Header is here.</div>

<div>hello world</div>

<div class="doNotPrint">
  my footer is here.
  <button (click)="onPrint()">Print</button>
</div>

to add some content in the template.

Then we write

//...
export class AppComponent {
  //...
  onPrint() {
    window.print();
  }
}

to add the onPrint method in our component.

It calls window.print to open the print dialog.

Then we write

@media print {
  .doNotPrint {
    display: none !important;
  }
}

to hide the stuff with the doNotPrint class in the printout.

Conclusion

To print HTML template in Angular and TypeScript, we call the window.print method.

Categories
JavaScript Answers

How to do group by in Sequelize and JavaScript?

Sometimes, we want to do group by in Sequelize and JavaScript.

In this article, we’ll look at how to do group by in Sequelize and JavaScript.

How to do group by in Sequelize and JavaScript?

To do group by in Sequelize and JavaScript, we call findAll with an object that has the group property.

For instance, we write

const result = await Table.findAll({
  attributes: ["column1", sequelize.fn("count", sequelize.col("column2"))],
  group: ["Table.column1"],
});

to call Table.findAll with an object that has group= set to ["Table.column1"] to group by Table‘s column1.

Then we use await to get the returned result.

Conclusion

To do group by in Sequelize and JavaScript, we call findAll with an object that has the group property.

Categories
JavaScript Answers

How to change lowercase characters to uppercase using the keyup event with JavaScript?

Sometimes, we want to change lowercase characters to uppercase using the keyup event with JavaScript.

In this article, we’ll look at how to change lowercase characters to uppercase using the keyup event with JavaScript.

How to change lowercase characters to uppercase using the keyup event with JavaScript?

To change lowercase characters to uppercase using the keyup event with JavaScript, we set the value to upper case with toUpperCase.

For instance, we write

const input = document.getElementById("inputID");

input.onkeyup = (e) => {
  e.target.value = e.target.value.toUpperCase();
};

to get the input with getElementById.

Then we set its onkeyup property to convert the input value to upper case with toUpperCase.

We then assign the returned string back to e.target.value to update the string.

Conclusion

To change lowercase characters to uppercase using the keyup event with JavaScript, we set the value to upper case with toUpperCase.