2017-09-13 4 views
0

フィールドに時間がないときの時間を表示しないでください。私は次のコードでHTMLの日付を結合してい

<p class="margin-right-3x"> 
    <i class="zmdi zmdi-calendar icon icon--white-icon "></i> 
    {{event?.date.start | date:'EEEE, MMMM dd, yyyy, h:mm a'}} 
</p> 

データベースは時々データベースは時間がそれに関連付けられており、以下のように見えるがない、時間と日付を持っている場合、これは正常に動作し、

enter image description here

時間がない場合はどうすれば省略できますか?パイプがありますか、どうすればこの状態を確認できますか?

+0

カスタムパイプを作成して、入力文字列の時刻部分がゼロでないかどうかをチェックし、datePipeトランスフォームを 'EEEE、MMMM dd、yyyy、h:mm a '形式で使用すると、otherwizeは' EEEE 、MMMM dd、yyyy'をパターンとして出力する。 – mok

+0

@mokサンプルを提供できますか? –

+0

@mok –

答えて

0

指定された日付の時間部分をチェックしてそれに応じて表示するフィルタを作成できます。私はnative Javascript Dateメソッドを使ってこれを作った。 JS

.filter('timeCheck', function($filter) { 
    return function (dateString) { 
      let userDate = new Date(dateString); 
      let UTCMinutes = userDate.getUTCMinutes(); 
      let UTCHours = userDate.getUTCHours(); 
      if(UTCMinutes === 0 && UTCHours === 0) { 
      const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }; 
      let dateWithoutTime = userDate.toLocaleDateString('en-us', options); 
      return dateWithoutTime; 
      } else { 
      const full_options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric' }; 
      let dateWithTime = userDate.toLocaleDateString('en-us', full_options); 
      return dateWithTime; 
      } 
    }; 
}); 

HTML

{{ vm.giveDate | timeCheck }} 

ここworking DEMO

私の提案はmoment.jsを使用することです。

関連する問題