I added to my MySQL table a column called "registerdate", the datatype of this column is Datetime (I tried also TIMESTAMP) and in DEFAULT I have CURRENT_TIMESTAMP.
The datetime comes automatically after registration, its showing the correct day month and year but its showing incorrect hour (-10 hours).
I hope someone knows how to fix it, thanks for helping.
From the MySQL documentation on TimeStamp (emphasis mine):
MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME.) By default, the current time zone for each connection is the server's time.
https://dev.mysql.com/doc/refman/5.5/en/datetime.html
Before you save the data into database, try to save with Culture Info like below
DateTime dt = DateTime.Now;
// CultureInfo for German in Germany.
CultureInfo ci = new CultureInfo("de-DE");
Console.WriteLine(dt.ToString("d", ci));
Related
Suppose the server is located in USA (MST) and an user from Chennai (IST) is making a payment. Both the server and user are in different timezones. The MST (Mountain Standard Time) is 7 hours behind GMT (Greenwich Meridian Time) and the IST (Indian Standard Time) is 5.30 hours ahead of GMT, that is both the server and user have a time difference of 12:30 hours. If the user is making the payment on 9/20/2011 9:00 AM then the time at the server is 8/20/2011 8:30 PM. So which time you have to record in the database? If the application stores the user’s time in the database then if someone from US is seeing the transaction will likely see a wrong time i.e. 8/20/2011 8:30 PM. The other option is storing the server’s time in the database (that what we normally do), then the user will be unhappy seeing a wrong time.
Can you please help me. Much appriciated.
I always use the UTC time to record everything, in any computer/server around the word.
DateTime.UtcNow
Then when I show the date and time to some user, I just change it base on their local time zone.
On saving any dates convert it into UTC and save into database, fore eg;
var now = DateTime.Now;
var Date = now.ToUniversalTime();
OR
var Date = DateTime.UtcNow;
and on displaying dates get the current Timezone of user and use it to convert the UTC date ,
var _Timezone = // store timezone id here
var southPole = TimeZoneInfo.FindSystemTimeZoneById(_Timezone);
DateTime userTime = TimeZoneInfo.ConvertTimeFromUtc(Date , southPole);
You can store the Timezone of different users in a database or get the current Timezone of the user logged in;
var _Timezone = new Date().getTimezoneOffset();
This method is not applicable in server side,
NOTE
You can get list of timezones from this code,
var timeZones = System.TimeZoneInfo.GetSystemTimeZones();
foreach ( var timeZone in timeZones )
{
Console.WriteLine( "{0} - {1}", timeZone.Id, timeZone.DisplayName );
}
I have a C# MVC Web application hosted in a remote server. I don't know the exact location of that.
The users of that application are all from (UTC + 06.00) Dhaka. When a user inserts a new record, I want the inserted datetime from his local time e.i (UTC + 06.00) Dhaka.
How can I do it?
The following code solves my problem-
DateTime utcTime = DateTime.UtcNow;
TimeZoneInfo BdZone = TimeZoneInfo.FindSystemTimeZoneById("Bangladesh Standard Time");
DateTime localDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, BdZone);
You can follow below approach:
Step 1:
You can store UTC time in database. If you are using SQL then you can use GETUTCDATE() to get UTC date.
Step 2
Please use Javascript to set a cookie, storing the browser timezone. (You can use scripts like jsTimeZoneDetect to get timezone name)
Step 3
Backend C# code:
Pull timezone from cookie.
Get the inserted utcTime from database and store in local variable(utcTime is the local variable name i used).
Use below mentioned code to convert UTC time to local time.
TimeZoneInfo tzoneinfo = TimeZoneInfo.FindSystemTimeZoneById("browser timezone name");
DateTime localTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, tzoneinfo);
Finally localtime is the end result. :)
Hope this will help you
Thank you
You should use global time zone of your ASP.NET application. Check this. This will work unless you use only C# datatime function. Once you have some complex code in SQL Server which will use GETTIME function. You will also rely on SQL Server timezone.
You should handle the internal datetime in UTC and convert the timezone only in UI.
I have retrieved a date from an application and stored it in a DateTime Variable. The format of date is dd/mm/yyyy.
I now want to update a column (with datatype date (yyyy/mm/dd)) in a sql server 2008 database with this date
I have tried the below code, but it's giving me an exception "string was not recognized as valid datetime". Please help to solve this problem.
DateTime date = calExpirydate.SelectedDate;
DateTime date1 = DateTime.ParseExact(date.ToString(), "YYYY/MM/DD", CultureInfo.InvariantCulture);
You don't need to convert it at all if you use parameters (and you should be).
A rough example:
DateTime date = DateTime.Now;
SqlCommand command = new SqlCommand();
command.CommandText = "INSERT INTO table (date) VALUES (#date)";
command.Parameters.Add("#date",SqlDbType.DateTime).Value = date;
I'm using SQL Server here, however the concept is similar across most ADO.NET providers.
Your DateTime variable in the framework is stored in one basic format. The way it appears is just formatting off of the .ToString. It's saying give me your date bit make it look like this. Sql server is similar, it understands the date time variable regardless of how it appears.
If you pass your DateTime as exactly how it is in the framework it will save it correctly. The date and time isn't changing just how it's displayed. Your try parse isn't working though because it's not able to recognize the partial string you're giving it.
You don't even need a new date time variable to see it the way you want. Even if you're successful you will have identical date time variables.
yyyy/MM/dd should be correct
DateTime string format
Am using DevExpresss Winform. I used DateEdit to store date and in access database I used datatype as Date/Time. Now I stored Date [19-11-2013] and then while retrieve the date is come with time. like this [19-11-2013 PM 12:00:00]. But I dont need time. How to solve this error ?
That's not an error. Even if in your database you have a Date data type you get a time when you use a DateTime variable in C#. What you see is the default time 12:00 PM or 00:00 in 24h format. Note that is not Date data type in C#.
If you don't want to see the time value, just format it accordingly
Standard Date and Time Format Strings
DateTime date1 = new DateTime(2008,4, 10);
Console.WriteLine(date1.ToString("d", DateTimeFormatInfo.InvariantInfo));
// Displays 04/10/2008
I am using C# and I am setting the value of the date time picker to my date from my database. But the format I have in database is different from the format of the Date Time Picker. Is there a way to automatically change the format of my database date to that of the date time picker. I have searched for it but I haven't found it.
I will appreciate your help.
But the format I have in database is different from the format of the Date Time Picke
You are making the mistake of thinking the database has any associated display format when storing a DATETIME - it doesn't - there is an internal representation. All you see is how the date is represented when you view it in some tool.
If your database uses a DATETIME column, as it should, then there is not issue.