简体   繁体   中英

Display 'Today' instead of date

I am using angular and moment.js to display dates in my app. If the user chooses a date other than today I want the format to be '12:00 PM Sat 21st Nov' which I have working when the user clicks a day from my calendar with amDateFormat the following:

$scope.setDate = function(day){
    var myDate = day.date;
    $scope.date = moment($scope.date).set(
    {
        'year': myDate.year(), 
        'month': myDate.month(), 
        'date': myDate.date()
    }).toDate();
}

<div> {{ date | amDateFormat:'h:mm A ddd Do MMM'}}</div>

If the date chosen is today how do I display the date to be '12:00 PM Today' instead?

I am not familiar with angular. However you can use moment calendar for today date. From the doc you can tweak the calendar like this:

moment.locale('en', {
    calendar : {
        sameDay : 'LT [Today]'
    }
});

And then call moment().calendar();

Here is a snippet with an alternative way of doing it assuming you are using a version that is at least 2.10.5:

 var divTime = $('#time'); var time = moment().calendar(null, { sameDay: 'LT [Today]' }); divTime.text(time); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js"></script> <div id="time"> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM