admin管理员组文章数量:1022864
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
1 Answer
Reset to default 3In 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';
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
1 Answer
Reset to default 3In 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';
本文标签: javascriptFormatting Date Time toDateString in TypescriptStack Overflow
版权声明:本文标题:javascript - Formatting Date Time toDateString in Typescript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745527069a2154573.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论