简体   繁体   English

如何将datepicker返回的日期格式转换为其他格式?

[英]How do I convert datepicker returned date format to a different format?

Sorry about the bad title, I didn't exactly know how to phrase that any better. 很抱歉,标题不好,我不知道该怎么说更好。 I'm going to try to explain what I need as best as possible. 我将尽力解释我所需要的。

I'm currently making a Task Manager (a ToDo list) in Javascript where you can create tasks, set a schedule for them and then you would have a list with all the tasks that you need to do and the respective schedule of each task (beginning and ending date). 我目前正在用Javascript创建任务管理器(待办事项列表),您可以在其中创建任务,为任务设置时间表,然后会有一个列表,其中包含您需要执行的所有任务以及每个任务的各自时间表(开始和结束日期)。

I'm using Angular.js and Angular Material to do my UI. 我正在使用Angular.jsAngular Material来做我的UI。 I have the Angular Material's datepicker. 我有Angular Material的datepicker。 This is the code I use to get the date value: 这是我用来获取日期值的代码:

angular.module('KBTM_App', ['ngMaterial']).controller('AppCtrl', function($scope) {
$scope.myDate1 = new Date();

...

// This is where I get the date value
var initial_date_1 = angular.element(document.querySelector('[ng-controller="AppCtrl"]')).scope().myDate1;

The problem is that this datepicker is returning the date in this format:"2016-04-29T08:36:10.027Z", which I don't want. 问题是此日期选择器以以下格式返回日期:“ 2016-04-29T08:36:10.027Z”,我不希望这样做。 I want a more normal format, like "DD-MM-YYYY". 我想要一个更普通的格式,例如“ DD-MM-YYYY”。

I tried converting the date format with Moment.js , but that didn't work. 我尝试使用Moment.js转换日期格式,但这没有用。 Here's the code I used for that: 这是我用于此的代码:

var initial_date = moment(initial_date_1, "YYYY-MM-DD'T'HH:MM:SS.SSS'Z'").format("DD-MM-YYYY");

And then I would store that date in the localStorage: 然后,我将该日期存储在localStorage中:

// Get the other dates already in the list
var initial_dates = get_initial_dates();

// Adds the new date to that list
initial_dates.push(initial_date);

// Stores those dates in the localStorage
localStorage.setItem('initial_date_db', JSON.stringify(initial_dates));

With that code I get " Invalid date " when trying to add a new task to the task list. 尝试将新任务添加到任务列表时,使用该代码,我会收到“ 无效日期 ”。

Sorry for the long post! 抱歉,很长的帖子!

Thanks to whoever can help me. 感谢任何可以帮助我的人。

您可以使用以下代码使用角材料datepicker转换html中的日期格式。

 data-ng-value="myDate1  | date : 'shortDate'"

Use the angular date filter to format the date. 使用角度日期过滤器格式化日期。 Refer the link angular date 参考链接的日期

var  initial_date = $filter('date')($scope.myDate1, "dd-MM-yyyy");

Inject the $filter in your controller main module. 将$ filter注入控制器主模块中。

Best way to handle dates is to use momentjs and there is a special AJS Directive available. 处理日期的最佳方法是使用momentjs并且有一个特殊的AJS指令可用。 angular-moment You can check [momentjs][2] offical docs for detailed info. 角时刻您可以检查[momentjs][2]官方文档了解详细信息。 You have to add angular-moment as dependency injection to use moment easily. 您必须添加angular-moment作为依赖项注入,才能轻松使用矩。 https://github.com/urish/angular-moment#instructions-for-using-moment-timezone-with-webpack https://github.com/urish/angular-moment#instructions-for-using-moment-timezone-with-webpack

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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