Format dates in response field in elastic search - elasticsearch

My server on which the application runs (uses ES) and ES are in central american time and I am accessing it from my server which is in IST.
I want some charts and data for today(Say Jan 3) so I send from date as today 12:00 AM to to date as current date in UTC to date range query. I get accurate number of records but I want the date to be shown as todays date alone(Jan 3), It gets divided between today(Jan 3) and yesterday(Jan 2) on client side access - ideally it should only todays date on client side for all records. Kibana renders correctly. So obviously there is a way and I am missing someting
I am using date range query - passing UTC for both to and from date and timezone offset as well.
Also, I am querying on a field which is of default type Date.I have not specified any custom date formats
Can somebody pleas help me with this?

Related

Changing format of date without using to_char - Oracle

I have to get the max payment date on an invoice and I am having trouble with the date format. I do not need the max in this formula as I am using the format in a reporting tool that is pulling the max from what it finds for me.
Using "to_char({datefield},'mm/dd/yyyy')" works for displaying that date the way we would like BUT when you use summary function MAX it does not pull the correct date because it is looking at a string and not a date (it will think 12/3/21 is larger than 3/2/22).
Another thing I have tried is trunc - "trunc({datefield})" which gives us the correct max date but it changes the formatting. For example if the date prior to the formula being applied is "8/12/21 12:00:00:000" the trunc formula will display it as 12-08-21 which is horribly wrong.
Long story short is I need a way to change a date/time to date with the format of 'mmmm/dd/yyyy' WITHOUT converting it to a string with something like to_char. Thank you!!!!
A DATE is a binary data type consisting of 7 bytes representing: century, year-of-century, month, day, hour, minute and second. It ALWAYS has all of those components and it is NEVER stored with any (human-readable) format.
What you are seeing when a date is displayed is the client application you are using to access the database making a decision to be helpful to you, the user, and display the binary DATE provided by the database in a human-readable format.
If you want to change how the DATE is displayed then you either need to:
Change the settings on the client application that controls how it formats dates when it displays them to you; or
Change the data-type so that it is no longer a DATE (which does not have a format) to a data type where the values of the date can be formatted (such as a string). You can do this using TO_CHAR.
If you want to find the maximum then do it BEFORE applying the formatting:
SELECT TO_CHAR(MAX({datefield}),'mm/dd/yyyy')
FROM your_table;

Laravel WhereDate Filter in Auth User Time zone

My default Laravel application timezone is America/Los_Angeles (pst), I'm storing all the timestamps like created_at with this timezone in database.
In the user profile, we are providing options to select a timezone. While showing the list of data for example in trip listing I'm converting & showing created at as per user selected time zone ( $date->setTimezone($user->timezone);)
For example, if the trip Id 197 has created_at 2020-06-11 23:00:00 stored in db (as per default application timezone i.e. pst) while in the listing I'm showing 2020-06-12 02:00:00 (est timezone as per user profile 3 hrs ahead).
Now everything works fine until I had to add date range (start & end date) filter in the listing. The problem is if I'm selecting start date 2020-10-12 in the filter, in result it is not getting 197 trip id because in the database it is stored as 2020-06-11 23:00:00., this 197 id record should be there in listing after filter because as per auth user timezone the trip is added on 2020-06-12. My DB query is $trips->whereDate('created_at', '>=' ,$request->start_date);. I have the only date and not time in request for filter trips I need to somehow pass timezone in this query or is there any better solution for this. The date filter should work as per user selected timezone
if anyone faced a similar problem following is the answer I found Generally for date range filters you’ll want to make sure you’re setting the start dates time to 00:00 and the end dates time to 23:59
if($request->filled('start_date'))
{
// $request->start_date;
$date = Carbon::parse($request->start_date, auth()->user()->timezone)
->startOfDay()
->setTimezone(config('app.timezone'));
$brokers->where('created_at', '>=' ,$date);
}
if($request->filled('end_date'))
{
$end_date = Carbon::parse($request->end_date, auth()->user()->timezone)
->endOfDay()
->setTimezone(config('app.timezone'));
$brokers->where('created_at', '<=' ,$end_date);
}
When you can have different timezones for 1 column in a table, you need dateTimeTz to store both datetime + timezone for each row.
With this, the whereDate will use the timezone stored, then you can reconvert to any other timezone on need without loss.

Why selecting a date\time range into Kibana the query is performed with wrong dates? URL parameters are wrong

I am working on a project involving ElasticSearch and visualizing data on Kibana and I am finding the following problem with date range selection.
As you can see in the following picture I selected I time range that starts from April 8 2021 at 00:00:00.000 and end at April 10 2021 00:00:00.000 . Then I clicked the Refresh button in order to perform the query.
The problem is that, as you can see in the highligher URL section the passed date range into the query is wrong !!! Infact this is the timerange section of my query into my URL parameters:
time:(from:'2021-04-07T22:00:00.000Z',to:'2021-04-10T10:00:00.000Z')
As you can see it is pretty differnt from what I selected into the start and end date form fields.
Changing the values into the start and end date form fields automatically changes also the values into the URL parameters but they still wrong.
What could be the problem? Why selecting dates and time into these form fields the parameters value are wrong? What could be the cause of this issue? How can I try to fix it?
I suspect something related to the server timezone (the server where Elastic\Kibana) are installed or something related to some Kibana configuration but I really have no idea
The kibana docs specify:
The timezone that Kibana uses. The default value of Browser uses the timezone detected by the browser.
So (I'm assuming) your browser default timezone is UTC + 2, meaning when you select 00:00 it's converted to 22:00 UTC timezone, The Z in 2021-04-07T22:00:00.000Z is in indicator here.

Control UTC time in server side Dynamicscrm

I saved a record in 17:16:15, I run a job which gets the ModifiedOn field of my record and I got- 15:16:15, my GMT is +2, I want to know how to fix that gap that my result will turn out like it should be - 17:16:15. I can't select it from DB I need a solution in server side (c# I mean) what can U do in that case?
DateTimes are always saved in UTC in the database. *
You need to dynamically convert from UTC into your local time zone. In C#, you can do this with the .ToLocalTime() method as long as your code is running in the correct time zone. You can also find your local time in the FormattedValues collection of the response, which uses your Dynamics timezone user settings . But the raw datetime value in the database will always be in UTC.
* The only exception to this is if the DateTime field is set to “TimeZone Independent” in the attribute type settings. But be careful: once you set this option you can’t change it for that field again.

Oracle SQL Output to Excel - Date format issue

I ran an SQL Query for Oracle which consists of Invoice date and Check date. When these data are copied on to an Excel Spreadsheet as text, it's dispayed as ex: "13-10-31" (Oct 31, 2013). However, when converted to date format, it's displayed as "10/13/1931". I've tried different date types but it always recognizes as the first part of the text as the day, then month, then year. I need these values to be setup as a date format as I need to calculate Days Payable Outstanding and other related ratios.
Is there any way to convert these values so that Excel recognizes the day, month, and year correctly? Would there be a macro that could automate this process for existing data and data that will be added in the future?
Thank you in advance.
Firstly, I hope the data type of your date column is DATE.
Secondly, the date should always have year as YYYY and not just YY. The world has already learned from Y2K bug.
If above two points are met, then while displaying use to_char(date_column, 'mm/dd/yyyy'). Thus, with YYYY format, there won't be any confusion between year and other fields.

Resources