在淘汰赛模板中格式化日期 [英] Formatting Date in Knockout Template

查看:35
本文介绍了在淘汰赛模板中格式化日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在淘汰赛模板中格式化日期.日期当前返回为

I'm wanting to format a date in knockout's template. The date is currently being returned as

2013-07-04T00:00:00

我希望它像这样显示

07/04/2013

这是我正在使用的绑定

<td data-bind="text: FirstDate">

它们在 Knockout 的模板中是默认的格式属性吗?

Are their default formatting properties in Knockout's template?

推荐答案

在 Knockout 中没有内置任何关于日期格式或一般格式的内容.text 绑定只是将属性值转换为字符串,因此如果您想要自定义格式,您需要自己完成.

There is nothing built in regarding date formatting or formatting in general in Knockout. The text binding just converts the property value to string so if you want custom formatting you need to do it yourself.

在 JavaScript 中处理日期并不是那么容易,所以使用像 moment.js 为此.它使用起来非常简单,它可以使用 format 格式化您的日期方法.内置格式 'L' 用于您所需的月份数字、月份日期、年份格式.

Working with dates is not so easy in JavaScript so you are probably better with using a third party library like moment.js for this. It is very simple to use and it can format your dates with the format method. There is built in format 'L' for your required Month numeral, day of month, year formatting.

您可以在视图模型中或直接在您的绑定中使用 moment js,例如:

You can use moment js in your view-model or directly in your binding like:

<td data-bind="text: moment(FirstDate).format('L')">

或者您可以创建一个自定义绑定处理程序来封装此格式化逻辑.

Or you can create a custom binding handler which encapsulates this formatting logic.

注意:确保在您的 FirstDate 属性上使用 () 如果它是数据绑定表达式中的 ko.observable获取它的价值.

Note: Make sure to use () on your FirstDate property if it is an ko.observable inside your data-binding expression to get its value.

这篇关于在淘汰赛模板中格式化日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆