Discussions

Expand all | Collapse all

How To Format A Date Returned From API?

  • 1.  How To Format A Date Returned From API?

    Posted 06-29-2015 12:15

    How To Format A Date Returned From API?



  • 2.  RE: How To Format A Date Returned From API?

    Posted 07-15-2015 14:14
    One impediment I see to people not using the API and script more is that they don't understand how to deal with date fields. When a date field is returned from the API it is not human readable - rather it is presented as an integer repenting  the number of milliseconds since the start of the Unix Epoch (midnight Jan 1, 1970). A typical date field returned by API_DoQuery might look like this:

    1435579444018


    Surprisingly, JavaScript does not have native features to convert this representation to a human readable format using patterns such as "mm/dd/yy". In the past I have used the wonderful library datejs to convert and manipulate dates:

    https://github.com/datejs/Datejs


    But it is just another library to load and in my work on the General Record Picker I wanted my interface to be screaming fast but still be able to format dates using the convenience of a library. Well it turns out that QuickBase uses the jQueryUI library on its forms and the date widget has methods to convert a date from the raw millisecond to a human readable format:

    >$.datepicker.formatDate("mm/dd/yy", new Date( parseInt("1435579444018" ,10)) );
    >"06/29/2015"

    Since this library is already loaded on QuickBase's pages you may as well use it. Here are the docs which show the wide range of human date formats you can generate:

    jQueryUI FormatDate Utility method
    https://api.jqueryui.com/datepicker/#utility-formatDate


  • 3.  RE: How To Format A Date Returned From API?

    Posted 03-05-2019 20:08
    Worked perfectly for what I needed. Thanks!


  • 4.  RE: How To Format A Date Returned From API?

    Posted 19 days ago
    Any tips on how to do this in Python? I've tried using the datetime module but for some reason it's not recognizing my date string, even though my dates are "mm/dd/yyyy" and I'm passing it the "%m/%d/%Y" format. I'm also using pyqb since I'm not that familiar with the QuickBase API yet.

    ------------------------------
    Jeff Jeff
    ------------------------------



  • 5.  RE: How To Format A Date Returned From API?

    Posted 19 days ago
    I'm assuming you are using something like date.strftime right now? From what I can see you need to use that function but you will need to convert the milliseconds into seconds so divide them by 1000 before trying to convert them into a date or datetime. This is the solution I have found.

    import datetime
    date
    = datetime.datetime.fromtimestamp(milliseconds/1000.0)
    date
    = date.strftime('%Y-%m-%d %H:%M:%S')


    You can also use Pandas to do this if you by any chance have it loaded.


  • 6.  RE: How To Format A Date Returned From API?

    Posted 19 days ago
    Thanks! I was able to convert the other way since I'm comparing dates from 2 different sources.

    datetime.utcfromtimestamp(float(str(my_date)[:10])).strftime('%m/%d/%Y')

    Am I missing something? Since I don't need the time of day I am getting matching dates, but this might not work if I needed the time of day.


    ------------------------------
    Jeff Jeff
    ------------------------------



  • 7.  RE: How To Format A Date Returned From API?

    Posted 05-18-2016 18:47
    Dan I'm trying to perform a calculation with a date field and the value I have stored is 5/26/2016 on the record. The API returns 1464220800000. The app setting is set to EST. I take this value and create a date with it and pass it to a function AddBusinessDays(d,n) d = date | n = # of days to add
    if you do $.datepicker.formatDate("mm/dd/yy", new Date( parseInt("1464220800000" ,10)) ); for me my console returns 5/25/16 at 8pm applying -4 hours for EST. so this date is whats being passed into the AddBusinessDays and that function does    d = new Date(d.getTime());  which returns 1464148800000. The timezone is getting applied twice. I find coding with dates really confusing for some reason. Any suggestions would be greatly appreciated!


  • 8.  RE: How To Format A Date Returned From API?

    Posted 05-18-2016 19:03
    I am at Google IO on a tablet so I can't type much. I would use datejs or moments (both used  by QB) - to manipulate dates.


  • 9.  RE: How To Format A Date Returned From API?

    Posted 05-18-2016 19:17
    When I calculate days I skip weekends but I also have an object with specific dates to exclude as well. Can I use that object with those as well?