Django change format for datetime in database - django-models

Can someone give a tips. How can i control format datetime for saving in db for my datetimefield with auto_now. It is saving in form YYYY-MM-DD HH:MM but i need that in database my entry looks like this 10/14/2015 1:34:54 AM.

Related

how to do date Localization using angularjs

I have model which has date in DD/MM/YYYY format i.e how it is saved in DB format.
Now while displaying to german user I Convert it DD.MM.YYYY and show it in textbox. But while saving back to DB i need to convert it in old format.
As i am using angular the new date is updated in model.
Could someone suggest or give some mock code for this.

Date format issue with vb6 on windows 7 and MS Sql database

I have a vb6 application and it linked to an MS SQL Database via Windows DSN. The application is using rdo connection to connect to the database and tables. Recently my users upgraded their OS to Windows 7. Now they have an issue with the dates whenever they do a posting or updates. Although the system default short date format is set as dd/MM/yy, the application keeps posting the dates to the database as mm/dd/yyyy.
The strange thing is that within the codes the date has already been formated per below
rsPO("LAST_UPDATED_DATE") = Format(Now, "DD/MM/YYYY hh:mm:ss")
so why is it still saving the date as mm/dd/yyyy?
Try with ISO format date. It is a universal date format and works in all DBs
rsPO("LAST_UPDATED_DATE") = Format(Now, "YYYY-MM-DD hh:mm:ss")
Dates are not saved in "dd/mm/yyyy" or any other particular format into the Database.
You just should make sure to use a format to save it in the same manner understood by the application, the OS and the DataBase
Then you can show it the format that you like.
ISO format works ok in most cases.
Date interpretation is a language thing. It's not set by the language on the client computer. It's set by the language of the login making the connection to the SQL database. There are a couple ways you can change things so that it is language independent.
Since LAST_UPDATED_DATE is a DateTime, you should be able to do this:
rsPO("LAST_UPDATED_DATE").Value = Now
If not, then you should format your dates like this: YYYYMMDD.
rsPO("LAST_UPDATED_DATE") = Format(Now, "YYYYMMDD hh:mm:ss")
Read more about it here: Setting a standard DateFormat for SQL Server
To make our developers into the context. The problem is not with stored Date in the database, but the problem happens when we call select for example select * from ... where Date_import ..... This date_import is getting into the database with the format mm-dd-yyyy by default. So the problem is with the String request SQL and not at all with your code or with how data is stored. The solution is to set all your code with YYYY-MM-DD instead of DD-MM-YYYY.

DateTime Parameters in Crystal Reports

I have written a stored procedure in SQLServer which accepts 2 parameters, both in dateTime format. The idea of the stored proc. is for the user to be able to search for specific cases between 2 different dates. The users are imputing the dates in mm/dd/yyyy format, and this returns data properly.
Now I need to turn this into a Crystal Report. I set up the report to use the Stored Procedure that I have written. When Crystal asks for the values of the 2 parameters, it will not accept them as mm/dd/yyyy format. It is trying to get me to put them in yyyy-mm-dd hr:mn:scds format.
Is there a way I can set up the report to accept the parameters as mm/dd/yyyy format?
Thanks in advance.
As far as I know you cannot control input format for the date parameters
In crystal report, you can right click the date variable and go to format, then choose the way you want the date to appear.
If the date is obtained correctly from the database, you can display it as you wish by changing the format.
Crystal Reports uses the datatype in the Stored Procedure. If you have a Date in your SProc Crystal will treat the value as a string and not display the calendar icon in the parameters popup. Solution is to change the Date to DateTime in your sProc and Crystal will display the calendar icon and format as yyyy-mm-dd automatically if your user clicks on the calendar icon. I know you probably want a DATE not DATETIME but use DATETIME I don't believe there is any other answer.

DateTime.Today value changed when sending to the server

I am working with Silverlight and I am getting a problem. In my Database I have stored some dates with a format like this yyyy/mm/dd 00:00:00, which means that I store only the year, month and day, getting the time to 00:00:00.
When the client performs an action and sends to the server I get the DateTime.Today which will keep the format of my database date, but when it is sendind I get yyyy/mm/dd 22:00:00, so when my server side function gets the date, it will return no values from the database.
How can I fix this to get the correct datetime?
Thank you
Use UTC times to make sure you don't run into timezone issues.
You can see if the DateTime is UTC or local based by checking the Kind property, and you can get the current UTC time by DateTime.UtcNow.
DateTime structure is very prone to DST, timezones and cultures when serialising it.
are you serialising it at client side before push it ? what is the difference between client and server timezones ?
I would suggest that you try and consume DateTime.UtcNow and then serialise the data. i prefer to serialise using Invariant culture
HTH
In addition to storing UTC time in the database, you can hard set the time to 12:00:00 for all date values by using the Date property of the DateTime class.
DateTime.UtcNow.Date
You can present the date to the user in their local timezone using the ToLocalTime method:
DateTime.ToLocalTime().Date
I'm sure every database engine has a similar function, but the SQL Server function to get UTC date is (surprisingly enough):
GETUTCDATE()
I hope this helps.

Date format problem using SSIS for Excel into SQL Server

I am trying to import a column of dates from a spreadsheet in Excel 2003 into SQL Server 2005 using SSIS. I am in the UK so want dates formatted as dd/MM/yyyy.
Unfortunately, the column in the spreadsheet contains a mixture of dates stored as strings in dd/MM/yyyy (with Excel 'General' formatting) as well as dates using Excel 'Date' formatting dd/MM/yyyy (with locale 'English (United Kingdom)').
This is just the way it is and I can't expect the users to be able to sort this out themselves.
When looking at the spreadsheet, all of the dates visually appear correct i.e. dd/MM/yyyy.
I am trying to import the values into a varchar column in a holding table in the database. Then I run a stored procedure that copies these values into the proper table which contains a datetime column.
The problem is that the dates that are stored in the spreadsheet and use Date formatting get imported as MM/dd/yyyy into SQL Server and the dates stored as strings are getting imported as dd/MM/yyyy. I have IMEX=1 in the connection string.
Having dates using both formats in the same varchar column is obviously causing a problem when I try to put it into a datetime column, hence
SET DATEFORMAT MDY;
SET DATEFORMAT DMY;
are of no use.
Does anyone know why the SSIS package would import the seemingly correct dates in the Excel spreadsheet into SQL Server as MM/dd/yyyy anyway?
Is there anyway to force the SSIS package to import the dates as dd/MM/yyyy that will work with this combination of dates as strings and cells with date formatting applied?
Any other ideas?!
Many thanks,
Anthony
I think you have answered your own question. The import of date formatted cells are treated as dates and others as a string. Possibly you SQL server date setting is MM/dd/yyyy and hence the conversion.
Why don't you try adding a data conversion step in you SSIS package and convert everyting in the column into a single format - datetime or string. Then I am sure SQL server will handle all of them the same way.
Raj
What worked for me was to add IMEX=1 to the Excel connection string.
So it will look like this:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Folder1\Book1.xls;Extended Properties="EXCEL 8.0;HDR=YES;IMEX=1";

Resources