ExtJS:以单独的格式提交日期

发布于 2025-01-02 09:20:57 字数 371 浏览 2 评论 0原文

我正在使用 ExtJS v4.0。

维护客户端和服务器端之间的日期格式确实令人困惑。

用户想要自己的输入格式,但服务器通常要求提交是一种标准格式。

它应该是 ExtJS 中的内置实现,但事实并非如此。

我已经阅读了有关此问题的一些解决方案,但他们只尝试解决 DateField 格式(通过创建特殊的 DateField 插件或扩展 SubmitAction)。

我正在寻找一个全局解决方案来格式化所有可以提交的日期(网格/商店编写器、表单/日期字段等)。

因此,日期可能根据区域设置具有自定义显示格式,但它们将以一种标准格式提交(例如 yyyy-mm-dd)。

对于这个问题有什么建议吗?

非常感谢。

I'm using ExtJS v4.0.

Maintaining date format between client and server side is really confusing.

Users want their own input formats, but the server usually requires the submission to be one standard format.

It should be a built in implementation in ExtJS but it's not.

I have read some solutions regarding to this issue but they only try to solve DateField formatting (by creating special DateField plugin or extending SubmitAction).

I'm looking for a global solution to format all dates which can be submitted (grid/store writer, form/datefield, etc).

So, dates may have custom display format according to locale but they will be submitted in one standard format (e.g. yyyy-mm-dd).

Any suggestion for this problem?

Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奢华的一滴泪 2025-01-09 09:20:57

这是我从使用版本 4.2.1 的文档中找到的唯一有效答案

    Ext.Date.patterns = {
    ISO8601Long:"Y-m-d H:i:s",
    ISO8601Short:"Y-m-d",
    ShortDate: "n/j/Y",
    LongDate: "l, F d, Y",
    FullDateTime: "l, F d, Y g:i:s A",
    MonthDay: "F d",
    ShortTime: "g:i A",
    LongTime: "g:i:s A",
    SortableDateTime: "Y-m-d\\TH:i:s",
    UniversalSortableDateTime: "Y-m-d H:i:sO",
    YearMonth: "F, Y"
};


//allow max date as today
Ext.override(Ext.form.DateField, {
    submitFormat:Ext.Date.patterns.ISO8601Short
});

This was the only working answer that I found from documentation using version 4.2.1

    Ext.Date.patterns = {
    ISO8601Long:"Y-m-d H:i:s",
    ISO8601Short:"Y-m-d",
    ShortDate: "n/j/Y",
    LongDate: "l, F d, Y",
    FullDateTime: "l, F d, Y g:i:s A",
    MonthDay: "F d",
    ShortTime: "g:i A",
    LongTime: "g:i:s A",
    SortableDateTime: "Y-m-d\\TH:i:s",
    UniversalSortableDateTime: "Y-m-d H:i:sO",
    YearMonth: "F, Y"
};


//allow max date as today
Ext.override(Ext.form.DateField, {
    submitFormat:Ext.Date.patterns.ISO8601Short
});
孤单情人 2025-01-09 09:20:57

我不知道任何通用的、在代码中实现一次的解决方案。但是您可以编写一个函数来迭代模式对象。有点像它在 < 末尾谈论 code>Ext.Date 文档中的对象介绍。您肯定需要添加更多模式,并且必须在需要的地方单独实现它,例如:对于商店编写器,您可以在 beforesync 事件中使用它,但对于日期字段,您可以将其作为验证器函数。我在不同的地方使用了这样的函数,这样用户就不会被绑定到特定的日期模式,该函数:

function reallyParse(aDate) {
    Ext.Array.each(Ext.Date.patterns, function(pattern) {
        aDate = Ext.Date.parse(aDate, pattern)
        return aDate === null;
    });
    if (myDate !== null) {
        return false
    } else {
        // format it the way your server wants it
        return Ext.Date.format(myDate, Ext.Date.patterns.ISO8601Long)
    }
}

我正在谈论的文档的具体引用是这样的:

以下是一些可能对您有用的标准日期/时间模式。
它们不是 Ext.Date 源的一部分,但要使用它们,您可以
只需将此代码块复制到之后包含的任何脚本中即可
Ext.Date,它们也将于该日期在全球范围内上市
目的。您可以根据需要在代码中随意添加或删除模式。

Ext.Date.patterns = {
    ISO8601Long:"Ymd H:i:s",
    ISO8601简称:“Ymd”,
    短日期:“n/j/Y”,
    长日期:“l,F d,Y”,
    FullDateTime: "l, F d, Y g:i:s A",
    月日:“F d”,
    短时间:“g:i A”,
    长时间:“g:i:s A”,
    SortableDateTime: "Ymd\\TH:i:s",
    UniversalSortableDateTime: "Ymd H:i:sO",
    年月:“F,Y”
};

使用示例:

var dt = new Date();
console.log(Ext.Date.format(dt, Ext.Date.patterns.ShortDate));

开发人员编写的自定义格式可以通过提供
格式化和解析功能,执行专门的
要求。这些函数存储在 parseFunctions 中并且
格式函数。

就像我说的,您必须添加到他们的模式对象中,并且实现会根据您使用它的位置而有所不同,但这就是我的做法。

I don't know any universal, implement once in your code, kind of solution to this. But you could write a function that would iterate through a patterns object. Kind of like it talks about at the end of the Ext.Date object intro in the docs. You would definitely have to add more patterns and you have to implement it individually where you wanted it, e.g.: For a store writer you could use it in the beforesync event but for a datefield you would put it as a validator function. I used a function like this in different places so that users wouldn't be tied to a specific date pattern, the function:

function reallyParse(aDate) {
    Ext.Array.each(Ext.Date.patterns, function(pattern) {
        aDate = Ext.Date.parse(aDate, pattern)
        return aDate === null;
    });
    if (myDate !== null) {
        return false
    } else {
        // format it the way your server wants it
        return Ext.Date.format(myDate, Ext.Date.patterns.ISO8601Long)
    }
}

The specific quote from the docs I am talking about is this:

Here are some standard date/time patterns that you might find helpful.
They are not part of the source of Ext.Date, but to use them you can
simply copy this block of code into any script that is included after
Ext.Date and they will also become globally available on the Date
object. Feel free to add or remove patterns as needed in your code.

Ext.Date.patterns = {
    ISO8601Long:"Y-m-d H:i:s",
    ISO8601Short:"Y-m-d",
    ShortDate: "n/j/Y",
    LongDate: "l, F d, Y",
    FullDateTime: "l, F d, Y g:i:s A",
    MonthDay: "F d",
    ShortTime: "g:i A",
    LongTime: "g:i:s A",
    SortableDateTime: "Y-m-d\\TH:i:s",
    UniversalSortableDateTime: "Y-m-d H:i:sO",
    YearMonth: "F, Y"
};

Example usage:

var dt = new Date();
console.log(Ext.Date.format(dt, Ext.Date.patterns.ShortDate));

Developer-written, custom formats may be used by supplying both a
formatting and a parsing function which perform to specialized
requirements. The functions are stored in parseFunctions and
formatFunctions.

Like I said, you would have to add to their patterns object and implementation would vary depending on where you're using it, but that's how I do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文