仅显示日期时间到分钟的最简单方法是什么

What is the simplest way to show datetime only till minutes?

本文关键字:最简单 方法 是什么 分钟 显示 日期 时间      更新时间:2023-09-26

我有一个类型为"datetime-local"的输入字段。当我为其分配当前时间戳时,输入文件也显示秒和毫秒。显示时间到分钟的最简单方法是什么?

  <body ng-controller="MainCtrl">
    <input type="datetime-local" class="form-control" id="DateTime" ng-model="formInfo.dateTime">  
  </body>

该应用程序.js是:

var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
   $scope.formInfo = {};
   $scope.formInfo.dateTime = new Date();
});

这是普伦克

尝试使用 http://momentjs.com/来格式化模型内容。

或者如果您使用的是角度日期选择器https://github.com/g00fy-/angular-datepicker

您已经有格式化日期的指令。

<input type="datetime" format="dd MMM yyyy" >

目前,您正在使用浏览器实现的日期时间选取器。 鉴于这在浏览器中不是标准的,没有使用另一个浏览器或为日期时间选择器提供一致的 api,据我所知,日期时间输入类型根本没有任何与之关联的选项。 以下是您如何轻松格式化 Date() 类型,因为我在注意到您正在谈论浏览器实现的日期时间选择器的显示之前发布了它。

设置历史参考的格式

toString().split()

.slice().join();

这可能不如仅使用它提供的某些功能对其进行格式化有效。 不过这很简单。

var a = (new Date()).toString().split(" ").slice(0,5).join(" ");
document.write(a);

按照Sandeep在评论中的建议使用日期过滤器:

  <body  ng-app="plunker" ng-controller="MainCtrl">
    <input type="text" class="form-control" id="DateTime" placeholder="Enter the date and time of consumption" ng-model="formInfo.dateTime| date:'yyyy-MM-dd hh:mm'">
  </body>