0

I would like to explain my problem of the day.

In the following code, everything works properly. The problem is I receive the "created" in the following format:

"created": "2020-02-18T13:45:01.652Z"

but I would only like to recover the time if possible without the seconds:

postbackend = () => {
  const config = {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      ...this.state,
      items: newItems,
      created: new
      Date().toISOString()
    }),
  };
  const url = entrypoint + "/alluserpls";
  fetch(url, config)
    .then(res => res.json())
    .then(res => {
      if (res.error) {
        alert(res.error);
      } else {
        alert(`ajouté avec l'ID ${res}!`);
      }
    }).catch(e => {
      console.error(e);
    }).finally(() => this.setState({ redirect: true }));
}

Do you have an idea of how to fix this? Neff

SparkFountain
  • 2,110
  • 15
  • 35
Neff
  • 199
  • 2
  • 17
  • 1
    Does this answer your question? [How to show only hours and minutes from javascript date.toLocaleTimeString()?](https://stackoverflow.com/questions/19407305/how-to-show-only-hours-and-minutes-from-javascript-date-tolocaletimestring) – demkovych Feb 19 '20 at 08:36
  • 1
    The `2020-02-18T13:45:01.652Z` is the format used by javascript for every `Date` object as a string. You can rebuild a `Date` object from this string using `new Date("2020-02-18T13:45:01.652Z")`. And then you can use this object to get date, month, hours, etc... (`date.getDate()` `date.getHours()`) You should look at this : https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Date and if you want something more casual, try `momentjs` it is a library made to play with date (compare date, format it, etc). – robinvrd Feb 19 '20 at 08:36
  • ``` var today = "2020-02-18T13:45:01.652Z"; console.log(today.split('').splice(11,22).join("")); ``` Try this. – Prakhar Mittal Feb 19 '20 at 08:44

3 Answers3

1

You are using the method Date().toISOString() which returns an ISO 8601 conform date string as explained in the official MDN documentation.

If you want to format the date without seconds, you should use another date format. In your case, you could define your own date format to make it look like the ISO string but without seconds and timezone for instance:

let today = new Date().toISOString().slice(0,16);
console.info(today);

(adapted from this StackOverflow post)

If you just want to receive the time without the date, use this instead:

let today = new Date().toISOString().slice(11,16);
console.info(today);
SparkFountain
  • 2,110
  • 15
  • 35
0
var today = "2020-02-18T13:45:01.652Z";
console.log(today.split('').splice(11,5).join(""));

Try this.

Prakhar Mittal
  • 6,315
  • 1
  • 14
  • 31
0

I would suggest you use moments js for anything to do with dates in javascript.

moment("2020-02-18T13:45:01.652Z").format('YYYY-MM-DD HH:mm') // 2020-02-18 19:15
ifelse.codes
  • 2,289
  • 23
  • 21