Argument of type 'Date' is not assignable to parameter of type 'string'
Asked Answered
H

4

29

I am working on Angular2-Meteor project and I am getting this

Argument of type 'Date' is not assignable to parameter of type 'string'.

error when I wrote this function to set value in date.

start: startOfDay(new Date(attendance.dayAt)),

I have tried to set the type as Date but won't work how can I resolve this error?

Hindward answered 28/12, 2016 at 5:18 Comment(14)
Give a little more detail please. What is startOfDay function?Donets
I am using RxJs map and javascript map to insert data in Angular2 Calendar, so it also provides events the "startOfDay" is the field in calendar events. so my inserting function looks like this ` return { start: startOfDay(new Date(attendance.dayAt)), title: attendance.status, color: this.getColor(attendance.status) } ` so how can I define the type of Date in startOfDay in typescript?Hindward
What is Angular2 Calender? A library?Donets
yes Angular2-Calendar is a moduleHindward
Any links to its documentation?Donets
Btw can you try start: startOfDay(new Date(attendance.dayAt).toString()) to see if it works?Donets
yes I tried but its not working and the link for "Angular2-Calendar" are : link linkHindward
I don't see any start config, I see startsAtDonets
but tell me how can I define of that? I mean to say that its working fine and everything is working fine, but the only problem is I am getting warning which I describe in the Head of this Question.Hindward
What I understand from your question is that either start: or startOfDay() (you even don't specify this) expects a string but you assign a Date. So the answer must be either startOfDay(new Date(attendance.dayAt).toString()) or startOfDay(new Date(attendance.dayAt)).toString()Donets
I tried it, but its not working.Hindward
@habib M.Farooq i think you are facing typescript error. new Date(attendance.dayAt) is of type data and startOfDay() is expecting a string. so can you try this startOfDay(new Date(attendance.dayAt)+'') you have already tried using . toString() and if its possible change startOfDay() argument type to anyOverload
I tried it and it also not working...Hindward
so what you will advice me to do? Please kindly resolve this issue...Hindward
S
26

I had a similar problem.

After researching I found three useful links :

Date variable works, but functions on it do not

Converting string to date issue

Invalid argument 'date format' for pipe 'DatePipe'?

What I then did was to declare my variable as type Date in the "export class" component like this :

start: Date

and then later on in a function I used it like this to populate the date variable:

start = new Date(Date.now());

Judging on the error you are receiving I assume that your function "startOfDay()" is returning a string so you would then have to change your code as follows.

start: new Date(startOfDay(new Date(attendance.dayAt)))

reason for this being that if "start" is a date then you have to use the function new Date() to assign a date to it. Date then takes string as input and returns a date and assigns it to variable "start". Hope this helps :)

Stheno answered 23/6, 2017 at 6:43 Comment(0)
S
3

I have a situation where a date value is being returned from a web service as a JSON string and a date is being cast into a Date type property in typeScript. This causes 'Type 'string' is not assignable to type 'Date'' error to be shown during the compilation.

One work-around I've found is to define client side property as 'any' type (instead of :Date). This allows JSON to nicely cast into a date and no error is shown during the compilation.

Samothrace answered 9/8, 2019 at 12:46 Comment(0)
H
0

if you are retrieving a Date as a string from an API you need to convert your angular date field from it:

getUser(id: number): Observable<User> {
    return this.http.get<User>(this.baseUrl + 'users/' + id.toString())
    .pipe(
      map( user => {
        user.lastActive = new Date(user.lastActive);
        return user;
      })
    );
  }
Hoad answered 23/10, 2019 at 21:6 Comment(0)
P
0

to set default as today's date use

const [scheduleDate, setScheduleDate] = useState<Dayjs>(dayjs(Date.now()));
Pignus answered 10/7 at 16:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.