繁体   English   中英

将JS格式数据扩展为“ DD-MM-YYYY”

[英]Ext JS Format DATA to “DD-MM-YYYY”

使用ExtJS 4.2.3 ,我具有带有文本字段的FORM。 我需要从商店到现场获取价值。 我尝试将数据格式化为所需格式时遇到问题。 寻求语法帮助将数据格式化为DD-MM-YYYY

资料来源: /Date(1417986000000)/

尝试通过以下方式获取数据后:

Form_window.query('textfield[name="DateField"]')[0].setValue(NewDATA_store.getAt(1).get('DateData'))

我得到的是: Mon Dec 08 2014 00:00:00 GMT+0300 (RTZ 2 (winter))

结果我需要什么: 08.12.2014

请尝试以下操作:

var inputDate = new Date('Mon Dec 08 2014 00:00:00 GMT+0300 (RTZ 2 (winter))');
var output = Ext.Date.format(inputDate, 'd.m.Y'); // will output: 08.12.2014

在可用的ExtJS Date单例类中,您需要使用Ext.Date.formate()方法。

Date类的一组有用的静态方法来处理日期。请注意,如果需要并加载Ext.Date ,它将为方便起见将所有方法/属性复制到此对象。

用法示例(请注意,必须使用'\\'转义格式说明符,以将其表示为字符文字):

//Current date
var dt = new Date();
console.log(Ext.Date.format(dt, 'Y-m-d')); // 2017-12-26
console.log(Ext.Date.format(dt, 'F j, Y, g:i a')); //December 26, 2017, 11:28 am
console.log(Ext.Date.format(dt, 'l, \\t\\he jS \\of F Y h:i:s A')); //Tuesday, the 26th of December 2017 11:28:02 AM

在此FIDDLE中 ,我使用form 'textfield'和store创建了一个演示。 我希望这会帮助您或指导您。

 Ext.create('Ext.data.Store', {
     storeId: 'dateStore',
     fields: [{
         name: 'DateData',
         type: 'date'
     }],
     data: [{
         DateData: new Date()
     }]
 });

 Ext.create('Ext.form.Panel', {
     title: 'Simple Form',
     bodyPadding: 5,
     width: 350,

     layout: 'anchor',
     defaults: {
         anchor: '100%'
     },

     // The fields
     defaultType: 'textfield',
     items: [{
         fieldLabel: 'Today Date',
         name: 'DateField',
         readOnly: true
     }],
     renderTo: Ext.getBody(),
     listeners: {
         afterrender: function (form) {
             var store = Ext.data.StoreManager.lookup('dateStore'),
                 date = store.getAt(0).get('DateData');

             form.down('[name=DateField]').setValue(Ext.Date.format(date, 'd.m.Y'));
         }
     }
 });

我使用ES6解决方案。 let就像var一样,但是仅获取声明let声明的上下文。 有时是一个函数,有时是for,如果... var声明获取其父函数的上下文。 这意味着不尊重前提,前提条件或其他功能。 我提供的功能正在运行。

let formatDate = (param) => {
  let day = param.getDate() < 10 ? '0' + param.getDate() : param.getDate();
  let month = (param.getMonth()+1);
  let year = param.getFullYear();

  return day +"."+ month +"."+ year;
}

let date = new Date("Mon Dec 08 2014 00:00:00 GMT+0300 (RTZ 2 (winter))");

window.console.log(formatDate(date));

暂无
暂无

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

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