How To Format A Date Returned From API?

  • 0
  • 1
  • Question
  • Updated 3 months ago
  • Answered

How To Format A Date Returned From API?

Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,074 Points 20k badge 2x thumb

Posted 4 years ago

  • 0
  • 1
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,074 Points 20k badge 2x thumb
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
Photo of Wendy Shoef

Wendy Shoef

  • 660 Points 500 badge 2x thumb
Worked perfectly for what I needed. Thanks!
Photo of itsjustchuck

itsjustchuck, Champion

  • 80 Points 75 badge 2x thumb
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!
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,074 Points 20k badge 2x thumb
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.
Photo of itsjustchuck

itsjustchuck, Champion

  • 80 Points 75 badge 2x thumb
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?