【问题标题】:Convert year in buddhist format将年份转换为佛教格式
【发布时间】:2015-02-05 10:53:38
【问题描述】:

我是 AngularJS 新手,有疑问。我使用Date.now() 创建当前时间。但我想以佛教格式显示年份。我使用来自本主题的代码 -> How to make a ticking clock (time) in AngularJS and HTML。如何将年份转换为佛教格式?谢谢大家。

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    你需要一个类似这样的过滤器:-

        myapp.filter('bdate', function($filter)
        {
         return function(input)
         {
          if(input == null){ return ""; } 
          var bYears=500;
          //Please write the formula for getting the buddhist date here
          //Below is a rough conversion of adding 365*bYears days to todays date
    
         // Convert 'days' to milliseconds
            var millies = 1000 * 60 * 60 * 24 * 365 * bYears;
         var d = new Date(input).getTime()+millies;
          var _date = $filter('date')(new Date(d), 'MMM dd yyyy');
    
          return _date.toUpperCase();
    
         };
        });
    

    上述过滤器的用法如下:-

     <span>{{dt | bdate}}</span>
    

    检查Plunkr here的代码。

    希望这会有所帮助!

    【讨论】:

    • 谢谢你。现在我使用 moment.js 并添加现在日期为 543 年。我也很棒。谢谢你再次回答。 ;)
    • 如果日期类似于“29/02/2016”(闰年),您将得到“1/03/2562”,因为 29/02/2562 不存在,因为 2562不是闰年,2 月 28 日是最后一天。而这个问题与momentjs相同,因为moment js使用的是new Date()。试试 moment('29/02/2016'),你会得到一个无效的日期。
    猜你喜欢
    • 1970-01-01
    • 2019-07-21
    • 1970-01-01
    • 2021-04-09
    • 2021-07-04
    • 1970-01-01
    • 2018-08-03
    • 1970-01-01
    • 2021-10-14
    相关资源
    最近更新 更多