I am trying to save date value to mysql DB, DB date column has UTCdatetime value, whenever the user selects time , it subtracts from the PST/EST/Mountain time, it works when i select PST & subtract -8 hrs, whenever i set MST- moutain time, -7, the date column doesn't save.
Any thoughts?
I'm not sure if I understood the question correctly, but if what you need is to store UTC dates, you don't need to manually add or subtract hours, there's a method for that:
var utcToday = DateTime.Today.ToUniversalTime();
Related
How I will restrict a user to enter only max date of the previous entered date?
I want to enter only onward date and block backward date in SQL and c#.net in windowform?
In the blow image textbox "Auto Most Recent Date (Last Date)" is backward date and textbox "Enter Next Date" is onward date.
You clearly know how to query a database, so run a query like:
SELECT MAX(somedate) FROM courtcases WHERE casenumber = #whatever
And use the output of it on your date time picker
nextDateDateTimePicker.MinDate = <the DateTime you queried>
If the min date is a day later, use AddDays(1) on the date you queried
(Not that that looks like a standard win forms datetimepicker but I’m sure whatever you’re using will have a similar facility)
I don’t think you need to go to the extent of protecting your insert sql against hacking to ensure the user hasn’t modified the ui in some way and put an illegal date in
I want to add posted date of product to database. I am using DateTime.UtcNow but it shows wrong date if date is wrong on computer. How can I solve this problem?
I have "prodpostDate" column in the "product" table and its type is nvarchar.
DateTime aDate = DateTime.UtcNow;
item.prodpostDate= aDate.ToString("dddd, dd MMMM yyyy");
_context.Products.Add(item);
The date is not wrong. The date is exactly what it should be. When you call Now functions, it pulls the date and time from the computer that executes the code. If the date is wrong on the computer, you need to update it/change the timezone.
Expanding on the comment below:
If you want the date and time regardless of a users local settings, you cannot get the date time from their local machine which is what happens if you call a Now function from code ran on the client. You need to make a call to a different source to get it. If you are using an API, you could make a call to it from your client to get the current date and time. Even better than that, if you end up sending a request to the server for an update, just don't send a date time and let the server get it and populate it. Or if you are doing a database update, let the database get the date time on update/insert.
I'm battling with my model for storing dates in a web application used in different time zones. At the moment, all dates are stored in UTC dates. So, lets say I am allowing the user to save a Transaction date, and a field on the screen is "Transaction Date". I default it to DateTime.UTCNow. SQL Server is in the United States, and I am in Australia.
So, what I have done now, is when I present the default transaction date, I add the GMT Offset to the UTC Now date:
var usersCurrentDate = DateTime.UtcNow.AddSeconds(GmtOffset.Value);
Where GmtOffset is a value in seconds, difference from GMT.
This code works - the user get's presented with the current date/time in their zone.
My issue is - how I should store this. Going to the UI, I do the above conversion. Should I then have a converted on the way to the database that converts the date entered by the user, back into UTC date/time?
In that case, I am always sure that all dates in the database are UTC dates. And then when ever I get the data from the database, I need to remember to translate into Local time? So, have a function that takes a Local time (As entered by the user) and converts it to the UTC?
Something like:
public DateTime UserDateToUTC(DateTime userDate)
{
return DateTime.UtcNow.AddSeconds(GmtOffset.Value * -1);
}
Is this an acceptable pattern?
In my ASP.NET (C#) project, I'm inserting some data into a database.
INSERT INTO USERS (username,join_date) VALUES('Ali',GETDATE());
Now when I'm fetching the date from the database, I'm using DateTime.
SqlDataReader r = Command.ExecuteReader();
r.Read();
myDate = r.getDateTime(0);
And when I'm inserting this in some DIV, I do this.
"<div>"+myDate.ToLongTimeString()+"</div>"
I get the correct date as in day/month/year, but the hour is always 12:00:00 AM.
How can I get the exact time?
make sure join_date is of type DATETIME and not DATE in the database
http://msdn.microsoft.com/en-us/library/ms186724.aspx
Using DATE as your column type means you are only storing the date not the time.
Therefore when you read the value back into a DateTime object the system has to initialise the time part - to midnight.
Convert the column to DATETIME and you'll get the time component stored as well.
For more information on the difference see the MSDN page on Date and Time Data Types and Functions
I have a date of birth field in the database which is of datatype date. I have three dropdowns for the same in my aspx page. One dropdown for day,one for month and one for year. If the user selects only day and month or month and year or any combination. How to insert that into the date field into the database.
If the user selects only the day and month or the month and the year or any combination as such. How to insert this into the database?
Thanks,
Two options:
Keep day, month and year in separate (nullable) fields in your database
Keep a DateTime field in the database, populate it with "dummy" data when it's not provided, and have a separate field to indicate what information was provided
The first sounds more sensible to me - it's not like you can really do many sensible date-related queries with "19th of some month in 1976", and it means your data accurately represents what the user specified.
Don't allow user to submit answer until he selects all the field. That's the standard way. you can use validator for it.
Or else try this(i am not sure about this)
1. Keep date field null.
And/Or
2.If (txtDate.Text = "") Then
cmd.Parameters("#Date").Value = sqldatenull
0000 isn't a valid year; firstly I'd revisit the requirements for the input form to decide what is optional and possible add validation.
If it is only the year that is optional then you could simply store 0001 as the year.
dt = new DateTime(Math.Max(1,dob_year), dob_month, dob_day);
If day and/or month are optional then a DateTime isn't the right data type because you cannot store null values so having separate fields for day, month, year is the only option.
A Date will always need all a day, month and a year because it refers to an actual day in the past, present or future.
All dates have to be in the range 01/01/0001 to 31/12/9999, so there is no way of storing the year 0000.
For your problem there are two options:
Store the day the, the month and the year in three separate fields.
Store the date in the usual way, setting the unspecified parts of the date to some default values. Then use a second field with a tinyint to store three bit flags which tell you which parts of the date are valid and which aren't