Skip to content

Date format of NOW() is different from DATE() #673

Open

Description

@tmrk

I've found that both GETDATE() and NOW() produce the following result:

NOW():              "2016.05.04 18:20:09.722"

However, when I'm using DATE([somedate]), I'm getting something like this:

DATE([somedate]):   Wed May 04 2016 18:26:16 GMT+0200 (CEST)

The problem is that I want to count the days between today and a future date, and for DATEDIFF to function the dates need to be in the same format.

At first, I thought I can just use DATE(NOW()). In Chrome that works, and gives me a result like the above, but in Firefox, Internet Explorer, and Safari it throws an error:

DATE(NOW()):        Invalid Date

Of course, I can solve this issue by using Date() or to make sure the formats match exactly: DATE(Date()) (both seem to work). So in the end, my code looks something like this:

alasql('SELECT DATEDIFF(Day, DATE([somedate]), DATE(Date()) ["Days Left"])',[somearray]);

...and this gives me the result I need (which in this case is the number of days left until somedate).

My question is, shouldn't NOW() generate a date in the same format as DATE() does, so that they can be compared easily?

Activity

  1. mathiasrw commented on May 4, 2016

    @mathiasrw
    Member

    If we get date(now) to work - would that solve it?

    I have to look into what other databasese gives as result for nowand date

  2. tmrk commented on May 4, 2016

    @tmrk
    Author

    If we get date(now) to work - would that solve it?

    I guess, although that might be a browser thing, because Chrome seems to be able to parse it properly already now.

  3. mathiasrw commented on May 4, 2016

    @mathiasrw
    Member

    Could you provide example of your data for me to see if there is a better solution for you?

  4. mathiasrw commented on May 4, 2016

    @mathiasrw
    Member

    Hmm. What do you get if you print alasql.version?

    I cant replicate.

    DATE(NOW()):        Invalid Date
    

    I get this from 0.2.5-develop-1262

    > alasql "select now()"  
    [
      {
        "NOW()": "2016.05.04 21:06:34.473"
      }
    ]
    > alasql "select date('20160712')"
    [
      {
        "DATE('20160712')": "2016-07-11T22:00:00.000Z"
      }
    ]
    > alasql "select  date(now())"     
    [
      {
        "DATE(NOW())": "2016-05-04T19:07:43.815Z"
      }
    ]
    
  5. tmrk commented on May 4, 2016

    @tmrk
    Author

    Well, as I wrote I am using DATE(Date()) to get today's date in the right format, so my code looks something like this: https://jsfiddle.net/fgwtu8e1/4/ (NB: I simplified it a lot to make it easier to read.)

    So the data in my database is stored in a YYYY-MM-DD format, which I convert to Javascript dates using new Date() and then feed it back into alasql's DATE().

    However, the solution DATE(Date()) seems a bit unnecessary, so I was wondering if the outputs could be more consistent.

    In the above jsfiddle I also included a demonstration of DATE(NOW()) failing. Please use a browser other than Chrome.

    ps. I'm using 0.2.6

  6. mathiasrw commented on May 4, 2016

    @mathiasrw
    Member

    In the above jsfiddle I also included a demonstration of DATE(NOW()) failing. Please use a browser other than Chrome.

    Woops...


    I have a feeling the good solution would be to let DATE() return today if nothing is set.

  7. tmrk commented on May 5, 2016

    @tmrk
    Author

    I have a feeling the good solution would be to let DATE() return today if nothing is set.

    I don't see any problem with that as long as it will work with DATEDIFF properly.

    Although it's not really standard: http://www.w3schools.com/sql/sql_dates.asp - is it? I'm not an SQL expert, so it's up to you guys. ;)

    Thanks for dealing with the issue!

  8. mathiasrw commented on May 7, 2016

    @mathiasrw
    Member
  9. self-assigned this
    on May 7, 2016
  10. removed their assignment
    on Nov 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions