Format date from database?
Normally the code is just:
echo date('F d, Y h:mA', strtotime('2009-10-14 19:00:00'));
Note that if strtotime()
can't figure out the date, it returns the time as 1/1/1970 00:00:00 GMT.
How to change format of date and insert it in database using php and html only
Thanks for help but I found the solution. We can simply enter the date by accepting input type='text' through HTML and store it directly in database and we can use this code while displaying data from database :-
$date = date('d-m-Y', strtotime($_POST["date"]));
Now we can see data in dd-mm-yy format now this works perfectly...
Get date from DB and format in C#
For date.ToString
with formatting to work, date needs to be a DateTime. Try changing your code to something like this:
DateTime date = (DateTime)dt.Rows[0]["declaration_date"];
string dateFormat = date.ToString("MM/dd/yyyy");
Also, your formatting string should be MM/dd/yyyy
.
How to change to display format of datetime received from a database in java
You don’t want to
You don’t want the datetime
in your database to have a specific format. Good practice in all but the simplest throw-away programs is to keep your user interface apart from your data model. The value of the datetime
belongs in your model, so keep your datetime
there and never let the user see it directly. When you adhere to this, it will never matter which format the datetime
has got in the database. Whenever the user should see the date, format it into a String
and show the string to the user. Similarly if you need a specific format for exchange with another system, format the datetime into a string for that purpose. If the user needs to enter a date and/or time, either accept a string or use a date picker or time picker.
You cannot
As @GuidoG has already said in comments, a datetime is stored in the database in some internal format that we don’t know and shouldn’t care about. It certainly isn’t the 2019-07-26 11: 53: 18.0
that you reported. You may have seen that in some query tool or as a result of retrieving the datetime as a string from your database, I don’t know. It may also be that you can configure your query tool to show you a different format. You can think of the datetime
in the database as a date and time of day, nothing more, nothing less.
In short “format” applies only to the string representations of datetimes, not to the datetimes themselves.
java.time and JDBC 4.2
The java.sql.Date
class that you were using is poorly designed and long outdated. Also your JDBC driver should treat it as a date without time of day, and most JDBC drivers do (so it’s a bit weird how you got your code to store 2019-07-26 11: 53: 18.0
with non-zero time of day).
Assuming that you want to store a point in time (a timestamp), change your database column to datatype timestamp with time zone
and then store an OffsetDateTime
into it. I am assuming that dates.get(i).getTime()
gives you the milliseconds since the epoch.
OffsetDateTime dateTimeToSave = Instant.ofEpochMilli(dates.get(i).getTime())
.atOffset(ZoneOffset.UTC);
psAttendence.setObject(2, dateTimeToSave);
If you cannot change the datatype, you may store a LocalDateTime
into your database column using psAttendence.setObject
in the same way, but you will have to make sure yourself that you get a LocalDateTime
in the right time zone since the object itself carries neither time zone nor offset. UTC is recommended.
Tutorial link: Oracle tutorial: Date Time explaining how to use java.time.
Converting date format javascript database
If the date formatt of the API is consistent, you could do this:
function formattDate(str) {
return str.split('T')[0].split('-').reverse().join('-')
}
That's the quickest solution. But if the formatt is not consistent, that is another case.
how to retrieve date format data from database
Well you need to change your query that you are firing on your database. Select the right format that you need.
You can try this .. convert(varchar(20),getdate(),101)
This will return in mm/dd/yyyy
102 - yyyy.mm.dd
103 - dd/mm/yyyy
104 - dd.mm.yyyy
105 - dd-mm-yyyy
etc.
Use Replace(convert(varchar(11),getdate(),106), ' ','/')
for DD/Mon/YYYY
format.
Format date from database to table using php
Solution:
echo "<td>" . date('d-m-Y', strtotime($row['Date'])) . "</td>";
Manual
Related Topics
Laravel's 5.3 Passport and API Routes
How to Detect If Have to Apply Utf-8 Decode or Encode on a String
Copy File from Remote Server or Url
Woocommerce - Send Custom Email on Custom Order Status Change
Xml Creation Using Codeigniter
Sending Correct File Size with PHP Download Script
Best Way to Periodically Execute a PHP Script
Why I am Getting Error When Getting Booking from Database
Targeting Specific Email with the Email Id in Woocommerce
PHP & MySQL: When Exactly to Use HTMLentities