How to declare a date attribute in an angular2 model

I use Rails 5 + Angular2 to build a web application, and I have a model called Books. My problem is that I have a file called "books.ts" that has code:

export class Book {
id: number;
author_one: string;
author_two: string;
author_three: string;
title: string;
subtitle: string;
publisher: string;
year: date;
city: string;
edition: number;
volume: number;
pages: number;
ISBN: string;
barcode: string;
}

But when I ran "ng serve --port 9000", I got the following error:

Cannot find name 'date'.

Before I ran into the same problem with other attributes, because I used "integer", but I changed it to "number", and it worked, so I wonder if the attribute types are obscure about Angular. But after searching the Internet, I did not find how to declare a variable of type "date" in Angular. Is there a way to declare a date type variable? Or should I use a string and have some sort of treatment to use it as a date?

+4
4

typescript. typescript date. date, Date javascript, . Angular/TS , Date. JSON, (JSON ).

+5

there is no date type in javascript, use the Date javascript type.

0
source

You can use moment.js to simplify.

import { Moment } from 'moment';

export class Book {
    id: number;
    author_one: string;
    author_two: string;
    author_three: string;
    title: string;
    subtitle: string;
    publisher: string;
    year: Moment;
    city: string;
    edition: number;
    volume: number;
    pages: number;
    ISBN: string;
    barcode: string;
}
0
source

Source: https://habr.com/ru/post/1675200/


All Articles