最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Formatting Date Time toDateString in Typescript - Stack Overflow

programmeradmin1浏览0评论

I searched through other questions, but couldn't find any other problem like this one.

I am learning to work with interfaces and classes in typescript. I am using the toDateString method, and I am getting the right output as far as the date string format, but I am not getting the right date that I have defined as parameters in the class instantiation. After brain storming and getting a migraine, I think I fixed the problem, but I'm not sure if it was the right approach, or if there is a better way.

This is my interface which it has the currentDate object and a printDate method defined and with a type of void.

interface appointment{
currentDate:Date;
printDate():void;

}

Then the class AppointmentDateFormatter which implements appointment interface. In the constructor I have defined number types to the year, month and day

class AppointmentDateFormatter implements appointment{
currentDate:Date;

constructor(year:number, month:number, day:number){
    this.currentDate = new Date(year, month, day );

Now here, I use the printDate() method with the type of void: to log to the console the date formatted using the toDateString() method.

}
printDate():void {
    console.log(this.currentDate.toDateString());
}

In the class instantiation I set the parameters of the date I want to log to the console.

const AppointmentDate = new AppointmentDateFormatter(2019, 10 , 28);

AppointmentDate.printDate();

But I get the wrong date back---> Thu Nov 28 2019 instead of Oct which it's the month it should've logged to the console. Since I noticed that it was giving me a month ahead, I decided to put a minus 1 to the number 10 and I thought it would make sense since the 10 is actually of type number, I would get as a result the actual month 10 which it's October and surprisingly it worked!

const AppointmentDate = new AppointmentDateFormatter(2019, 10 - 1 , 28);

I'd just like to know if this approach is the right one, and if there was something else I could've done that would be considered a more proper way of acplishing the expected result. I'd truly appreciate your answers.

This is the entire code with the (2019, 10 - 1, 28) moderation included.

<script src=".js/1.7.5/angular.min.js"></script>


interface appointment{
    currentDate:Date;
    printDate():void;

}

class AppointmentDateFormatter implements appointment{
    currentDate:Date;

    constructor(year:number, month:number, day:number){
        this.currentDate = new Date(year, month, day);
        

}
    printDate():void {
        console.log(this.currentDate.toDateString());
    }
}

const AppointmentDate = new AppointmentDateFormatter(2019, 10 - 1 , 28);

AppointmentDate.printDate();

I searched through other questions, but couldn't find any other problem like this one.

I am learning to work with interfaces and classes in typescript. I am using the toDateString method, and I am getting the right output as far as the date string format, but I am not getting the right date that I have defined as parameters in the class instantiation. After brain storming and getting a migraine, I think I fixed the problem, but I'm not sure if it was the right approach, or if there is a better way.

This is my interface which it has the currentDate object and a printDate method defined and with a type of void.

interface appointment{
currentDate:Date;
printDate():void;

}

Then the class AppointmentDateFormatter which implements appointment interface. In the constructor I have defined number types to the year, month and day

class AppointmentDateFormatter implements appointment{
currentDate:Date;

constructor(year:number, month:number, day:number){
    this.currentDate = new Date(year, month, day );

Now here, I use the printDate() method with the type of void: to log to the console the date formatted using the toDateString() method.

}
printDate():void {
    console.log(this.currentDate.toDateString());
}

In the class instantiation I set the parameters of the date I want to log to the console.

const AppointmentDate = new AppointmentDateFormatter(2019, 10 , 28);

AppointmentDate.printDate();

But I get the wrong date back---> Thu Nov 28 2019 instead of Oct which it's the month it should've logged to the console. Since I noticed that it was giving me a month ahead, I decided to put a minus 1 to the number 10 and I thought it would make sense since the 10 is actually of type number, I would get as a result the actual month 10 which it's October and surprisingly it worked!

const AppointmentDate = new AppointmentDateFormatter(2019, 10 - 1 , 28);

I'd just like to know if this approach is the right one, and if there was something else I could've done that would be considered a more proper way of acplishing the expected result. I'd truly appreciate your answers.

This is the entire code with the (2019, 10 - 1, 28) moderation included.

<script src="https://cdnjs.cloudflare./ajax/libs/angular.js/1.7.5/angular.min.js"></script>


interface appointment{
    currentDate:Date;
    printDate():void;

}

class AppointmentDateFormatter implements appointment{
    currentDate:Date;

    constructor(year:number, month:number, day:number){
        this.currentDate = new Date(year, month, day);
        

}
    printDate():void {
        console.log(this.currentDate.toDateString());
    }
}

const AppointmentDate = new AppointmentDateFormatter(2019, 10 - 1 , 28);

AppointmentDate.printDate();

Share Improve this question asked Oct 23, 2019 at 3:33 joesalazjoesalaz 551 silver badge7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

In typescript or javascript date month start with (0 to 11). hence when you pass new Date(2019,10,28) means this is Nov month.

So, you have to write the logic in a way that new Date() should give you the right date..

and instead on .ToDateString(), I will suggest you to use angular datePipe.

this.datepipe.transform(new Date(2019,10,28) , 'yyyy-MM-dd') // syntax for datepipe

make sure if you have injected the datepipe in angular class constructor.

Full Example

export class AppComponent {

  constructor(
    private datePipe: DatePipe,
  )
  convertDateToFormat() {
    const dateIs = this.datePipe.transform(new Date(2019, 10, 28), 'yyyyMMdd');
  }
}

Note: import { DatePipe } from '@angular/mon';

发布评论

评论列表(0)

  1. 暂无评论