2017-02-25 5 views
0

に私は次のようになり、この方法には、この無効な日時は、活字体

private  modifyDate(dateString: string): Date { 
    let formattedDate= new DatePipe(Defaults.APPLICATION_LOCALE).transform(new DateIEPipe().transform(dateString), 'shortDate'); 
    let newFormattedDate: Date = new Date(formattedDate); 
    return newFormattedDate; 
} 

入力などの日付を変更する方法を持っている「2017-01-23T14:09:19 + 0100」、すなわち文字列

私はタイムゾーンを持つために文字列をフォーマットするパイプを使用しています。

@Pipe({ 
    name: 'dateIE' 
}) 
export class DateIEPipe implements PipeTransform { 

    transform(input:String): any { 

        if(input.trim() == "") return input; 
        // Check if timezone present in the date string? 
        let splits = input.split("+"); 
        let formattedTZ = ""; 
        if(splits.length > 1) { 
            formattedTZ = splits[1].substr(0, 2).concat(":").concat(splits[1].substr(2)); 
            return splits[0].concat("+").concat(formattedTZ); 
        } else { 
            return input; 
        } 
    } 
} 

このパイプは、2017年1月23日午前9時19分のような日付ストリングが返されます。今私はこれをDateに変換したいので、 "let newFormattedDate:Date = new Date(formattedDate);"私は無効な日付として取得しています。これを解決するには?

+0

あなたのDefaults.APPLICATION_LOCALEは何ですか? –

+0

@VivekSinghロケールを設定するためのもので、デフォルトロケールをen_USに設定しました – Protagonist

答えて

1

あなたのコードに基づいて私はplnkr hereを作成しました。それは私に必要な出力を与えています。

DatePipe詳細については

private modifyDate(dateString: string): Date { 
    let d = new DatePipe('en-US'); 
    return d.transform(new DateIEPipe().transform(dateString),'short'); 
} 

Angular docsを参照してください、あなたのプライベート関数を修正

、およびExamples here

ため、それがお役に立てば幸いです。

+0

出力を "23.01.2017 09:19"と期待していますが、出力が "Mon Jan 23 2017 00:00:00 GMT + 0530(インド標準時) " – Protagonist

+0

私はplnkrをチェックアウトして更新しました... https://plnkr.co/edit/9bD64nQ3n0KJWm5S8MiO?p = preview –