Wednesday, December 27, 2017

mydatepicker doesn't take a Date

You may see partway down https://github.com/kekeh/mydatepicker that...

public model: any = { date: { year: 2018, month: 10, day: 9 } };

 
 

...is what is expected as a TypeScript type and not Date. Another change is that herein ten really means October and not November. That JavaScript ugliness of the zero to eleven scale for months is replaced with something sane. Unfortunately, this means you may to have translate from the sane to the unsane and back again begging the question of any sanity to begin with. (Forgive the use of the word any.) I wrote a pipe around this stuff today to format the fake dates in a MM/dd/yy shape.

import { Pipe, PipeTransform } from '@angular/core';
import { PseudodateWrapper } from './pseudodate-wrapper.model';
@Pipe({name: 'simplePseudodate'})
export class SimpleDateForPseudodatePipe implements PipeTransform {
   transform(value: PseudodateWrapper): string {
      if(value && value.date){
         let month:string = value.date.month + "/";
         if (value.date.month < 10) month = "0" + month;
         let day:string = value.date.day + "/";
         if (value.date.day < 10) day = "0" + day;
         let year:string = String(value.date.year);
         return month + day + year[2] + year[3]
      }
      return "";
   }
}

 
 

This is kinda cool, huh? What is above also shows off casting a number to a string. PseudodateWrapper looks like this:

import { Pseudodate } from './pseudodate.model';
export class PseudodateWrapper {
   date:Pseudodate;
}

 
 

It may be overkill but I am trying to get away from the overuse of any. Pseudodate in turn looks like:

export class Pseudodate {
   day:number;
   month:number;
   year:number;
}

No comments:

Post a Comment