I have dates stored in datetime in an MSSQL database. In my C++ application, these dates are pulled out of the database and stored in a CString.
I am trying to use COleDateTime's ParseDateTime() to display these dates in a nice format. However I found they are being displayed in american format (MM/DD/YYYY), and sorted as such. I would like them displayed in UK format (MM/DD/YYYY). I have attempted the code below to no difference:
codt.ParseDateTime(sqlresults.GetItem(_T("date"),l),0,MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_UK));
Thanks!
The ParseDateTime function is used to read the provided value – in your case you get it from the database and set the internal representation of the COleDateTime object.
You are not after this method but rather after the COleDateTime::Format method.
codt.ParseDateTime(
sqlresults.GetItem(_T("date"),l),
0,
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US));
CString dateAsUkString = codt.Format(_T("%d/%m/%Y"));
// display the dateAsUkString string in your view
Related
I have a column with dates (in a string format) in Dataprep: yyyymmdd. I would like it to become a datetime object. Which function/transformation should I apply to achieve this result automatically?
In this case, you actually don't need to apply a transformation at all—you can just change column type to Date/Time and select the appropriate format options.
Note: This is one of the least intuitive parts of Dataprep as you have to select an incorrect format (in this case yy-mm-dd) before you can drill-down to the correct format (yyyymmdd).
Here's a screenshot of the Date / Time type window to illustrate this:
While it's unintuitive, this will correctly treat the column as a date in future operations, including assigning the correct type in export operations (e.g. BigQuery).
Through the UI, this will generate the following Wrangle Script:
settype col: YourDateCol customType: 'Datetime','yy-mm-dd','yyyymmdd' type: custom
According to the documentation, this should also work (and is more succinct):
settype col: YourDateCol type: 'Datetime','yy-mm-dd','yyyymmdd'
Note that if you absolutely needed to do this in a function context, you could extract the date parts using SUBSTRING/LEFT/RIGHT and pass them to the DATE or DATETIME function to construct a datetime object. As you've probably already found, DATEFORMAT will return NULL if the source column isn't already of type Datetime.
(From a performance standpoint though, it would probably be far more efficient for a large dataset to either just change the the or create a new column with the correct type versus having to perform those extra operations on so many rows.)
I have faced this problem in other projects, but right now we're working in Django / Python with Vue.js and PostgreSQL.
Nearly every 'date' field in our model is truly a date, not a datetime. The business users don't care about a time-part, in fact storing any such value is misleading. A good example is the effective date on a tax rate. From the user point of view, it takes effect at midnight on the specified date.
If we store this as a Django DateTimeField or DateField, it requires a time-part. It could be 0000h, but the value has no meaning to the business, because they will never need to see or compare anything with the time-part. The Django functionality is great if we want to store a real datetime, put it into the database as UTC, and then display it to users in their local time through the automatic time zone conversion. But we don't. If the effective date is March 1, 2019, it should display as such regardless of the user's timezone. If the date is stored as a datetime (2019, 3, 1, 0, 0, 0) and entered by someone in Vancouver, it will appear as the next calendar day for another user in Toronto. Definitely not what we want. We could kludge it by setting the time-part to 1200h, but really?
We also have potential problems, depending on the internal representation in the database, when using SQL or tools that access the schema directly (e.g. BI tools). How do we know what time zone applies to the datetime value?
So, we're thinking of using Django CharField with ISO 8601 format (YYYY-MM-DD) instead. It will sort properly, it can be compared easily (or directly in some tools like SQL), and can be displayed without reformatting if the client is willing to use the standard. If we need to do date arithmetic, we can use the Python Standard Libraries datetime and calendar to convert from/to string. We'll need to use those to catch SQL injection attacks anyway.
We will also need to deal with date entry through a Datepicker, converting to the ISO 8601 string before storing and back again when displaying for edit.
It appears to be a better way to represent what the business needs, and it gets rid of any timezone conversion issues.
There is certainly a lot of comment on datetime and time zone handling, but I haven't found anyone taking this approach to storing true dates. Am I missing an important 'gotcha'? We're early enough in the project that we can go either way, so I'm hoping to confirm that this will work before refactoring becomes a big job.
Have you considered using DateField?
This will only store the date part and not the time.
I'm trying to use ember-pikaday, but I'm having issues with it not displaying the correct date. Every time the date it displays, its the previous date of the actual date. For example if the date is 04/06/2016, it displays 04/05/2016. Below is the code that I am using, I have noticed though that if I replace value with placeholder, then the date does display correctly, just not in the right format, i.e. 2016-04-06. What's going on here?
{{pikaday-input id="effectiveDate" name="effectiveDate" class="form-control textCenter" format="MM/DD/YYYY" value=model.effectiveDate firstDay=0 disabled=isDisabledDate}}
This is because it uses your local timezone by default when creating a new Date object. The addon supports working with UTC, try setting useUTC=true on the component.
In U2/Universe/Pick, there is a function ICONV("11/11/15","D") to convert an external date to an internal date but this will work in USA or with the date format mm/dd/yyyy only. How can I use this function for UK date format (dd/mm/yyyy) ?
ICONV("11/12/15","D4/DMY")
output: 17512
According to the UniVerse BASIC Commands Reference documentation, you can also use the following to control the date format:
You can set the default date format with the DATE.FORMAT command. A
system-wide default date format can be set in the msg.text file of the
UV account directory. Date conversions specified in file dictionaries
or in the ICONV function or the OCONV function use the default date
format except where they specifically override it. When NLS locales
are enabled, the locale overrides any value set in the msg.text file.
NLS sets the local date format required so if it is enabled the following does not apply.
The question is, do you want all dates in dd/mm/yyyy format or just for this process. Assuming all dates should be, then setting DATE.FORMAT ON at login will do this. Otherwise you can set it temporarily just for the process and afterwards set it back using DATE.FORMAT OFF.
I need to match on a date using the LIKE operator. The date should be in current user's local format, not in the general format.
So I use the strftime function like this:
WHERE strftime('%d.%m.%Y %H:%M', createdDate, 'localtime') LIKE '%{searchTerm}%'
Unfortunately this works only for fixed format '%d.%m.%Y %H:%M'. But I want to use the user's current locale format.
So I need to either:
1) Get the strftime format string from a C++ locale object
2) Make SQLite format the date using current locale itself
Spent already several hours on this to no avail.
Would be grateful if anyone would point me in the right direction on how to tackle this problem.
Example:
Given current locale is German I want to get something like "%d.%m.%Y %H:%m".
For an US locale I want to get "%m/%d/%Y %H:%m"
Normally the local date format can be obtained with the Windows GetDateFormat API (or GetDateFormatEx API for Vista). Your program could interrogate the API then transform the date accordingly. Following that, the date can be recorded in SQLite.
However, once can question the validity of storing timestamps in a specific format. That basically means a lot of code to manipulate each date, or no date manipulation at all. May I suggest, if it is possible, storing in a plain format (say ISO or UNIX timestamp) and working from that, outputing with whichever flavour of GetDateFormat is required?
OK, different answer.
Suppose you have your MyTable:
CREATE TABLE MyTable (
MyPrimaryKeyHnd INTEGER PRIMARY KEY,
...
CreatedDate TEXT);
(Where CreatedDate is in ISO format. I suppose you could also use a Unix timestamp. Your choice.)
Then a list of possible formats
CREATE TABLE TimeFormat (
TimeFormatHnd INTEGER PRIMARY KEY,
TimeFormatString TEXT NOT NULL,
TimeFormatDescriptor TEXT);
You allow your user to chose a format of their choice and keep that in a seperate table or INI file. TimeformatString would be your strftime() compatible format string (such as '%d.%m.%Y %H:%M'). You just need to build your query with whatever the user's choice is.