angularjs中的日期格式不合适

时间:2015-09-15 04:40:13

标签: javascript angularjs

我有时间输入字段我得到的时间是1970年1月1日星期一12:59:00 GMT + 0530(印度标准时间)这样我只想要时间。为什么我得到这样的是有任何问题在angularjs的时间选择器任何人帮助我?



function HistoryCtrl($scope) {

     $scope.updateForm = function () {
        console.log($scope.booktime);
       //here i am getting Thu Jan 01 1970 12:59:00 GMT+0530 (India Standard Time)
        
    };

    
}

<div ng-app ng-controller="HistoryCtrl">
    
     <div class="col">
            <label class="item item-input item-stacked-label">
            <span class="input-label" >Time</span>
            <input type="time" ng-model="booktime" name="booktime" min="09:00:00" max="21:00:00" required="">
            </label>
            <div class="form-error" ng-messages="projectForm.booktime.$error">
               <div class="form-error" ng-message="required">* Mandatory</div>
               <div class="form-error" ng-message="min">Booking times: 9am - 9 pm</div>
               <div class="form-error" ng-message="max">Booking times: 9am - 9 pm</div>
            </div>
         </div>
   
    <button ng-click="updateForm()">Update</button>
   
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

您可以将moment.js用于处理日期时间。

http://momentjs.com/

或使用

对于angularjs

https://github.com/urish/angular-moment

格式日期

moment().format('MMMM Do YYYY, h:mm:ss a'); // September 15th 2015, 10:30:07 am
moment().format('dddd');                    // Tuesday
moment().format("MMM Do YY");               // Sep 15th 15
moment().format('YYYY [escaped] YYYY');     // 2015 escaped 2015
moment().format();                          // 2015-09-15T10:30:07+05:30
Relative Time
moment("20111031", "YYYYMMDD").fromNow(); // 4 years ago
moment("20120620", "YYYYMMDD").fromNow(); // 3 years ago
moment().startOf('day').fromNow();        // 11 hours ago
moment().endOf('day').fromNow();          // in 13 hours
moment().startOf('hour').fromNow();       // 30 minutes ago
Calendar Time
moment().subtract(10, 'days').calendar(); // 09/05/2015
moment().subtract(6, 'days').calendar();  // Last Wednesday at 10:30 AM
moment().subtract(3, 'days').calendar();  // Last Saturday at 10:30 AM
moment().subtract(1, 'days').calendar();  // Yesterday at 10:30 AM
moment().calendar();                      // Today at 10:30 AM
moment().add(1, 'days').calendar();       // Tomorrow at 10:30 AM
moment().add(3, 'days').calendar();       // Friday at 10:30 AM
moment().add(10, 'days').calendar();      // 09/25/2015
Multiple Locale Support
moment().format('L');    // 09/15/2015
moment().format('l');    // 9/15/2015
moment().format('LL');   // September 15, 2015
moment().format('ll');   // Sep 15, 2015
moment().format('LLL');  // September 15, 2015 10:30 AM
moment().format('lll');  // Sep 15, 2015 10:30 AM
moment().format('LLLL'); // Tuesday, September 15, 2015 10:30 AM
moment().format('llll'); // Tue, Sep 15, 2015 10:30 AM

答案 1 :(得分:0)

Angularjs有一个function HistoryCtrl($scope, $filter) { $scope.updateForm = function () { console.log($filter('date')($scope.booktime, 'mediumTime')); }; } 服务,提供日期格式。

为方便起见,我提供了以下示例:

# This file is - if you set up HUGE correctly - not needed.
# But, for fallback reasons (if you don't route your vhost to /public), it will stay here.
RewriteEngine on
RewriteRule ^(.*) public/$1 [L]

# Everything from is for browser caching and is totally optional

# Deflate Compression by FileType


<IfModule mod_deflate.c>
 AddOutputFilterByType DEFLATE text/plain
 AddOutputFilterByType DEFLATE text/html
 AddOutputFilterByType DEFLATE text/xml
 AddOutputFilterByType DEFLATE text/css
 AddOutputFilterByType DEFLATE text/javascript
 AddOutputFilterByType DEFLATE application/xml
 AddOutputFilterByType DEFLATE application/xhtml+xml
 AddOutputFilterByType DEFLATE application/rss+xml
 AddOutputFilterByType DEFLATE application/atom_xml
 AddOutputFilterByType DEFLATE application/javascript
 AddOutputFilterByType DEFLATE application/x-javascript
 AddOutputFilterByType DEFLATE application/x-shockwave-flash
</IfModule>

# Set browser caching to 1 month
<IfModule mod_expires.c>
 ExpiresActive On
 ExpiresByType text/css "access plus 1 month"
 ExpiresByType text/javascript "access plus 1 month"
 ExpiresByType text/html "access plus 1 month"
 ExpiresByType application/javascript "access plus 1 month"
 ExpiresByType image/gif "access plus 1 month"
 ExpiresByType image/jpeg "access plus 1 month"
 ExpiresByType image/png "access plus 1 month"
 ExpiresByType image/x-icon "access plus 1 month"
</IfModule>

<ifmodule mod_headers.c>
 <filesmatch "\\.(ico|jpe?g|png|gif|swf)$">
  Header set Cache-Control "max-age=2592000, public"
 </filesmatch>
 <filesmatch "\\.(css)$">
  Header set Cache-Control "max-age=604800, public"
 </filesmatch>
 <filesmatch "\\.(js)$">
  Header set Cache-Control "max-age=216000, private"
 </filesmatch>
</ifmodule>

您可以通过this链接获得所需的时间格式。

希望这会有所帮助:)