2

I have an SDK which returns the time in yyyy-mm-dd't'hh:mm:ssZ this format, but I just want the Day, Month and Time.

I searched it over and didn't found an exact solution to my problem. Can anyone help?

Rob
  • 164
  • 1
  • 2
  • 12
  • Have you tried using a SimpleDateFormatter? – gkgkgkgk Jul 30 '18 at 13:53
  • 1
    "I just want the Day, Month and Time": in which time zone? – Dormilich Jul 30 '18 at 13:54
  • try [momentjs](https://momentjs.com/) – phuzi Jul 30 '18 at 13:55
  • `I just want the Day, Month and Time` <= It's right there in that string. What are you actually trying to do? Do you want to display it? Parse it? Store it? Calculate a new `DateTime`? Something else? Define `convert to "normal time javascript"`... – Igor Jul 30 '18 at 13:57
  • @Dormilich the format ends with a `Z` indicating UTC – phuzi Jul 30 '18 at 13:58
  • @phuzi That's the input format, my question was if the output format should be UTC as well or something different. – Dormilich Jul 30 '18 at 14:00
  • Is there a way to know the TimeZone of a user? – Rob Jul 30 '18 at 14:03
  • The browser will always know it and by default it will be displayed in the local timezone. – phuzi Jul 30 '18 at 14:05
  • Is there a way, to compare today's date with the returning date from the API using moment.js? – Rob Jul 30 '18 at 14:17
  • To do what exactly? See if they are the same? See if one is bigger than the other? Get the number of days or minutes or seconds difference between the 2? The easiest thing to do here is for *you* to do some more research in what it is you are needing to do and see if *you can do it yourself*. Then if you get stuck come back with your code so far (see [mcve]) and a *clear problem statement* (see [ask]. – Igor Jul 30 '18 at 14:21

3 Answers3

6

You can use moment for sure but you can use Date API to achieve it. The format you have mentioned it's ISO string. You can pass it directly to JavaScript Date method and use it in following way:

const formated_Date = '2017-07-30T15:01:13Z';
const date = new Date(formated_Date) // formated_Date - SDK returned date

console.log(`${date.getDate()}, ${date.getMonth() +1 }, ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`)

This will return you Date, Month, HH:MM:SS

Sandip Nirmal
  • 2,283
  • 21
  • 24
1

Easy enough using momentjs, it already understands the ISO8601 formats for time and date strings.

let str = '2018-07-30T15:01:13Z';
let date = moment(str);

console.log(date.format('llll'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/moment.min.js"></script>

Take a look at the docs and you can format it however you need.

phuzi
  • 12,078
  • 3
  • 26
  • 50
0

use moment.js. Its simple to format date, for example: moment(dateStringHere).format('DD:MM:YYYY');

looffee
  • 81
  • 1
  • 8