Results 1 to 6 of 6

Thread: Ext.Date.format() function gives date.getTime error

  1. #1
    Sencha User
    Join Date
    Jun 2017
    Posts
    25
    Answers
    2

    Default Answered: Ext.Date.format() function gives date.getTime error

    Hello there,

    I've a function and trying to testing if it's being success with it's aim (through unit-test approach) but it keeps fail with this error;
    Code:
    Uncaught TypeError: date.getTime is not a function    at Object.clone (VM644 ext-modern-all-debug.js:4446)
        at Object.add (VM644 ext-modern-all-debug.js:4484)
        at constructor.dateAdd (VM653 app.js:11)
        at constructor.launch (VM653 app.js:18)
        at constructor.onBeforeLaunch (VM644 ext-modern-all-debug.js:57409)
        at constructor.onProfilesReady (VM644 ext-modern-all-debug.js:57342)
        at constructor (VM644 ext-modern-all-debug.js:57312)
        at new constructor (VM644 ext-modern-all-debug.js:9184)
        at VM644 ext-modern-all-debug.js:57576
        at Object.invoke (VM644 ext-modern-all-debug.js:12386)
        at Object.doInvokeAll (VM644 ext-modern-all-debug.js:12426)
        at Object.invokeAll (VM644 ext-modern-all-debug.js:12395)
        at Object.handleReady (VM644 ext-modern-all-debug.js:12363)
        at VM644 ext-modern-all-debug.js:12373
        at Object.elevate (VM644 ext-modern-all-debug.js:2054)
        at timerFn (VM644 ext-modern-all-debug.js:4983)
    Here is snippet of method;
    Code:
    Ext.application({    name: 'Fiddle',
    
    
        dateAdd: function (theDate, theInterval, theValue) {
            var me = this;
    
    
            theDate = theDate || me.today();
            theInterval = theInterval || Ext.Date.DAY;
            theValue = theValue || 1;
    
    
            return Ext.Date.add(theDate, theInterval, theValue);
        },
    
    
        launch: function () {
            var me = this;
    
    
            // Formated dateAdd() version
            var dt         = me.dateAdd(Ext.Date.format(new Date('01.02.2018'), 'd.m.Y'), Ext.Date.DAY, 1)
    
    
    
            // Aim to see: '03.01.2018' as displayed date.
            Ext.Msg.alert('Date is...', dt);
        }
    });
    as well you can find the running code through this Fiddle.
    Thanks in advice.

    ===
    UPDATE

    In a short way when Ext.Date.add method wraps format() method;

    Code:
    Ext.Date.add(Ext.Date.format(new Date('01.02.2018'), 'd.m.Y'), Ext.Date.DAY, 1);
    Returns as;
    Code:
    Uncaught TypeError: a.getTime is not a function
    In docs says for Ext.Date.add();
    // Basic usage:var dt = Ext.Date.add(new Date('10/29/2006'), Ext.Date.DAY, 5); console.log(dt);// returns 'Fri Nov 03 2006 00:00:00'
    So it returns as string and with time, timezone values.
    How can I be able to format those values to only Day/Month/Year such as 29.10.2006 (or M/D/Y such as 10.29.2006)?

  2. '

    • I could write a test assertion w/o any format but I wanted to use format method to get a global return such as d.m.Y. As you know otherwise it keeps returning time and timezone values which are changeable through time/location.

    '
    I totally agree with you concerning difficulties about timezone and location, but what is your expectation when running the test case in the browser or on your ci system with jasmine or whatever? If you want to instruct your test system to behave like a client at some loacation/timezone, simply take new Date() out of the race. Invoke Ext.Date.parse() to ensure that no system specific routines will be called for your test case, otherwise, when you invoke new Date(), you have to handle with timezones and locations, of course. Depends on your needs. @see https://youtu.be/-5wpm-gesOY
    of a guy which makes his hair turn gray :-)

  3. #2

    Default

    Hi, you cannot format your time back to a string and then try to do Ext.Date.add. It is enough to parse the date and call add like this way:
    Code:
    Ext.Date.add(Ext.Date.parse('01.02.2018', 'd.m.Y'),Ext.Date.DAY, 1);
    Greetings.

    By the way, if the format of your date is sth. like german as dd.mm.YYYY new Date() interprets in this way : mm.dd.YYYY. So in my opinion it is not worth to use the Ext.Date.parse function.
    Code:
    new Date('02.03.2018')
    Sat Feb 03 2018 00:00:00 GMT+0100 (CET)
    To format to some specific format, do this as last step:

    Code:
    Ext.Date.format(Ext.Date.add(Ext.Date.parse('01.02.2018', 'd.m.Y'),Ext.Date.DAY, 1),'d.m.Y')

    BTW, forked your fiddle and made it a little bit shorter by es6: https://fiddle.sencha.com/#view/editor&fiddle/2bk9
    Last edited by aicgroup-dev; 6 Jan 2018 at 9:20 AM. Reason: fiddle

  4. #3
    Sencha User
    Join Date
    Jun 2017
    Posts
    25
    Answers
    2

    Default

    Dear @aicgroup-dev thanks a lot for your advice. I've examine your sample code usage. It's pretty good solution to get date within d.m.Y format but unfortunately it's not suitable for me. I'll try to explain why it is not;


    • My approach is writing a proper unit-test for dateAdd() method (you can find it in my first post). The dateAdd method already uses and return with Ext.Date.add() so through unit-test's aims I should be able to testing/checking if the dateAdd() method doing it's duty w/o any problem.



    • Therefore as I know, I can not wrap dateAdd method with Ext.Date.format (It's not ok for unit-test's aim). Instead of this I have to use it (Ext.Date.format) or another solution inside of dateAdd() method. Below you may see unit-test assertion.



    • I could write a test assertion w/o any format but I wanted to use format method to get a global return such as d.m.Y. As you know otherwise it keeps returning time and timezone values which are changeable through time/location.



    • So far I learnt there is no solution to format the given date inside Ext.Date.add() method, right?


    Code:
    t.describe('dateAdd() method', function(t) {
        t.it('should interval the given date', function(t) {
            t.expect(MyApp.dateAdd(Ext.Date.format('01.01.2018', 'd.m.Y'),Ext.Date.DAY, 1)).toBe('02.01.2018',);
        });
    });

  5. #4
    Ext Support Team
    Join Date
    Nov 2017
    Posts
    46
    Answers
    2

    Default

    As rightly pointed out by @aicgroup-dev

    Ext.Date.add() functions expects a Date object as the first argument. If we use Ext.Date.format('01.01.2018', 'd.m.Y') which returns a string, it throws an error. So You can use either new Date() or Ext.Date.parse('01.02.2018', 'd.m.Y')


    Thanks
    Aravind.

  6. #5

    Default

    '

    • I could write a test assertion w/o any format but I wanted to use format method to get a global return such as d.m.Y. As you know otherwise it keeps returning time and timezone values which are changeable through time/location.

    '
    I totally agree with you concerning difficulties about timezone and location, but what is your expectation when running the test case in the browser or on your ci system with jasmine or whatever? If you want to instruct your test system to behave like a client at some loacation/timezone, simply take new Date() out of the race. Invoke Ext.Date.parse() to ensure that no system specific routines will be called for your test case, otherwise, when you invoke new Date(), you have to handle with timezones and locations, of course. Depends on your needs. @see https://youtu.be/-5wpm-gesOY
    of a guy which makes his hair turn gray :-)

  7. #6
    Sencha User
    Join Date
    Jun 2017
    Posts
    25
    Answers
    2

    Default

    Dear aicgroup-dev that was pretty clear. Thanks for advice =)

Similar Threads

  1. Replies: 4
    Last Post: 20 Jun 2017, 4:28 AM
  2. Ext.date.format unable to format yang:date-time
    By vikramjit.singh in forum Ext JS 6.x Q&A
    Replies: 2
    Last Post: 20 Oct 2016, 6:07 AM
  3. Replies: 2
    Last Post: 18 Jun 2016, 9:27 PM
  4. Replies: 0
    Last Post: 22 Apr 2013, 11:41 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •