After googling around, I cannot find a way to create a new table with a DATETIME column with the default format set to 'DD-MM-YYYY HH:MM:SS'
I saw a tutorial in which it was done in phpmyadmin so I suspect that I could use mysql via command line and achieve the same thing when creating my new table with
CREATE TABLE ()
Thank you in advance
"MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format."
This is from mysql site. You can store only this type, but you can use one of the many time format functions to change it, when you need to display it.
Mysql Time and Date functions
For example, one of those functions is the DATE_FORMAT, which can be used like so:
SELECT DATE_FORMAT(column_name, '%m/%d/%Y %H:%i') FROM tablename
Use DATE_FORMAT function to change the format.
SELECT DATE_FORMAT(CURDATE(), '%d/%m/%Y')
SELECT DATE_FORMAT(column_name, '%d/%m/%Y') FROM tablename
Refer DOC for more details
As others have explained that it is not possible, but here's alternative solution, it requires a little tuning, but it works like datetime column.
I started to think, how I could make formatting possible. I got an idea. What about making trigger for it? I mean, adding column with type char, and then updating that column using a MySQL trigger. And that worked! I made some research related to triggers, and finally come up with these queries:
CREATE TRIGGER timestampper BEFORE INSERT ON table
FOR EACH
ROW SET NEW.timestamp = DATE_FORMAT(NOW(), '%d-%m-%Y %H:%i:%s');
CREATE TRIGGER timestampper BEFORE UPDATE ON table
FOR EACH
ROW SET NEW.timestamp = DATE_FORMAT(NOW(), '%d-%m-%Y %H:%i:%s');
You can't use TIMESTAMP or DATETIME as a column type, because these have their own format, and they update automatically.
So, here's your alternative timestamp or datetime alternative! Hope this helped, at least I'm glad that I got this working.
i have used following line of code & it works fine Thanks.... #Mithun Sasidharan
**
SELECT DATE_FORMAT(column_name, '%d/%m/%Y') FROM tablename
**
I'm pretty certain that you can't change the datetime format in mysql. The phpmyadmin setting is probably applying a custom format as it reads the datetime (using DATE_FORMAT or something from php). It shouldn't matter what format the database uses, format in the application to display it as you wish.
Date formatting is a pretty common task. I typically like to abstract it out into internationalization code or, if you don't need to deal with i18n, into a common date utility library. It helps keep things consistent and makes it easier to change later (or add i18n support).
No you can't; datetime will be stored in default format only while creating table and then you can change the display format in you select query the way you want using the Mysql Date Time Functions
This cannot be done for the table; besides, you even cannot change this default value at all.
The answer is a server variable datetime_format, it is unused.
Dim x as date
x = dr("appdate")
appdate = x.tostring("dd/MM/yyyy")
dr is the variable of datareader
try this:
DATE NOT NULL FORMAT 'YYYY-MM-DD'
DataTables properly sort dates in YYYY-MM-DD format only. All other formats they sort as string.
As far as I'm not a fan of adding a library / plugin for every functionality into my projects I tried to solve it by myself.
DataTables are using Date.parse() function to "understand" the date and sort it. (according to https://datatables.net/blog/2014-12-18).
So I decided to override that function and modify it in the way it will "understand" also other date formats.
I added this JS into my code:
let origFunction = Date.parse;
Date.parse = function(str) {
// I want to parse this date format: 27.01.2018
if (str.indexOf('.') > 0) {
str = convertDateToISO(str); // my function translates date into 2018-01-27
}
return origFunction(str);
};
When I test this in the browser console, Date.parse works perfectly fine with my date format, but DataTables keep sorting my european dates as strings.
Any idea what I'm doing wrong? Or is it possible to do it this way?
SOLVED:
After all it looks like really the easiest way is to include "Moment.js" plugin as described here: https://datatables.net/blog/2014-12-18#Operation
But while there was a "no plugin" requirement in my question, I'm accepting answer of #billynoah as a solution too.
You probably want to take a look at orthogonal-data. This allows you to define a custom value for sorting your column. In this case, the most straightforward thing to do is use a timestamp for sorting and then you can display date in any format you want. For data sourced from an object or ajax you can structure your column definition like:
{
data: 'date',
render: {
_: 'display',
sort: 'timestamp'
}
}
For html source you can use the data-order attribute:
<td data-order="1545466488">Sat, Dec 22nd 2018</td>
(Disclaimer / Attribution: This is all more or less straight out of the manual linked above)
I expect that stringResult will give me an output that corresponds to the specified format, but it always looks like YYYY-MM-DD HH:MM
Full code here.
For example, I've tried the following config:
{
view:"datepicker",
stringResult:true,
format:"%Y-%F-%d"
},
But the output is still the same. So, is it a bug? Or am I doing something wrong? Thank you in advance!
[Updated]
As mentioned by Loj, I agree with it. Thus, there are 2 solutions possible:
1. Custom Format
The format property you have used sets a date format to display in the datepicker field. So, it is just the display format and it is not actually formatted. Hence, the stringResult returns date as string with the default format.
In order to get the custom date in the output, you are required to add your custom format which will convert the date in the desired format.
var format = webix.Date.dateToStr("%Y-%F-%d");
Check the snippet here.
2. Using getText()
Using stringResult property in the control's configuration makes the getValue method to return raw unformatted value. Hence, instead you should use the getText() as
$$("custom").getText();
in your code to get the formatted output via stringResult.
With libary moment there is option to bring a array of formating options and momentjs use the best match for parsing the input.
For Example:
var date = moment("29-06-1995", ["MM-DD-YYYY", "DD-MM", "DD-MM-YYYY"]);
but what if I want the take the same format that using in parsing for output formating.
var dateText = date.format('selected parse')
How do I know which format moment choose to use?
Currently there is no exposed function for getting the chosen format, however there is a "private" field named _f that contains this information.
var m = moment("29-06-1995", ["MM-DD-YYYY", "DD-MM", "DD-MM-YYYY"]);
m._f // "DD-MM-YYYY"
If you use this, be careful when updating versions of moment. Private fields are not guaranteed to be maintained, and could break between versions.
I've logged this as a feature request for future moment.js functionality.
I am trying to use the date.format and getting an error that format is not a recognized method. I tried to use thesame in new solution, and I'm getting a correct result. Any idea why .format method is not working? These both are in Javascript.
Assuming you are talking about Date type...
It is clear why format method is not working - because there is no such method on Date object.
It is less clear why/when it would work - most likely you are using some script library that modifies Date object to have format method.
But if you are talking about some other type - format function may be present (or added) to any object and it is not possible to suggest anything without code...
I.e. custom object with format function:
var date = { format:function() {alert("Hi");}};
date.format();