1

In typescript, I am setting an object's date field in following way.

at: new Date("2017-06-24"+"T"+"22:00"+"Z")

Then I am sending that object as POST body to my webservice. In body, date field looks like "at":"2017-06-24T22:00:00.000Z" But I want o get rid of milliseconds part. How can I do that ?

luckystones
  • 301
  • 4
  • 14
  • you've `zero` milliseconds in date already. no ? – Muhammad Usman Jun 23 '18 at 22:16
  • yes its already zero – luckystones Jun 23 '18 at 22:18
  • y dont you directly use `at: '2017-06-24T22:00Z'`, you anyway are sending a string then y do you want to assign the value of at as a new Date() instead directly generate a string with the help of date and time which you already have. – Avinash Jun 23 '18 at 22:25
  • Hi @Avinash, because I tried to simplify question. I am getting date and time parts from a form input. Thats why I didnt ask question as you wrote – luckystones Jun 23 '18 at 22:29
  • have a look at this [remove milliseconds from date](https://stackoverflow.com/questions/35758963/remove-seconds-milliseconds-from-date-convert-to-iso-string) – Avinash Jun 23 '18 at 22:32
  • Unfortunately, the solution in following threat ends up with milliseconds :( https://stackoverflow.com/questions/35758963/remove-seconds-milliseconds-from-date-convert-to-iso-string – luckystones Jun 23 '18 at 22:37
  • i have one more doubt i mean even if you are getting the date and time from a form input you can use `{inputDate}T{inputTime}Z` to generate a String. – Avinash Jun 23 '18 at 22:43
  • I am using angular 2 form. and getting values like form.value.depAt and form.value.depTime. So I am not sure putting variables side to side means concatenation – luckystones Jun 23 '18 at 22:49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/173691/discussion-between-avinash-and-luckystones). – Avinash Jun 23 '18 at 22:50

1 Answers1

1

You can format the String you have from the form input values as

new Date("2017-06-24"+"T"+"22:00"+"Z").toISOString().split('.')[0]+"Z"

else you can directly generate the String from your form input values

`${this.form.value.Date}T${this.form.value.Time}Z`

in both the above cases you have the String output as 2017-06-24T22:00:00Z and not the Date type.
Date constructor in Js will have the milliseconds field so it will be an issue if at in your Object is defined as a Date instead of a String.

Avinash
  • 1,245
  • 11
  • 19