I am Trying to show a date in a textbox with short format, i get the data from the database.
At presentation Time I want it to be shown with another Format Than this
dd/MM/YYYY HH:mm:ss AM/PM
How can I do it?
Where I want it to be shown:
TextBox1.DataBindings.Clear()
TextBox1.DataBindings.Add("Text", n.ADataSet, "Table.date")
Inside the database it has rhis format:
No, inside the database it will be stored in some binary format. Fundamentally, it's a date - it's not in any particular format, any more than a number is stored in decimal or hex... it's just a date.
When you fetch the value, you're getting a DateTime. Again, that's not got any particular format - it's just a date and time. You need to format it at presentation time.
If you look at the documentation for ControlBindingsCollection you'll see an example with custom formatting.
Try something similar to the below... note that this overload allows you to specify a format ("yyyy-MM-dd") in the last parameter.
textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", n.ADataSet, "Table.date", true, System.Windows.Forms.DataSourceUpdateMode.OnValidation, null, "yyyy-MM-dd"));
Related
normally i parse date like this way
DateTime.Parse()
DateTime.ParseExact()
i am in situation where user run exe and pass date as argument. so user can give date with various format like
dd/MM/yyyy
MM/dd/yyyy
dd-MM-yyyy
MM-dd-yyyy
yyyyMMdd
so i have to parse that date. when date format is yyyyMMdd then i am parisng date like this way DateTime.ParseExact(this.enddate, "yyyyMMdd", CultureInfo.InvariantCulture).ToString("yyyy-MM-dd");
so guide me what code i should write to parse date which work for any date format. thanks
I would recommend that you standardize on a single format. Otherwise you will run into ambiguous dates in cases where you have dates that can be parsed by different formats, but represent different dates in both
Ex:
dd-MM-yyyy
MM-dd-yyyy
what code i should write to parse date which work for any date format
As a technical answer, you can pass multiple formats to DateTime.TryParseExact() via a string array containing all acceptable formats.
Practically, though, the others have already pointed out that there is no way to tell the difference between months and days when the format isn't strictly enforced.
One possible solution is to have the user pass the date in as three separate arguments, each flagged with some kind of indicator such as /y2013 /m11 /d12 or maybe y:2013 m:11 d:12. You can even mash them together like /y2013/m11/d12. Then you can use Regular Expressions to parse out the parts, or even just plain old string manipulation.
There's no built in way to parse dates which work for ANY format. However, you can quite easily define your own format using DateTimeFormatInfo, letting you convert more or less any format to a proper date, as long as you know the format ahead of time.
In every major website you enter the date using comboboxes for day/month/year
or some datetime widget. So I don't see a reason to use a textbox. If you really
need to, add a tooltip or a watermark with the predefined format and force the
user to it.
I'm new to MySQL and C#.
I stored certain values in a column with data type Date. I did not want the time, only the date to be stored.
On viewing these values using phpMyAdmin or MySql command line, I see them in the format:
YYYY-MM-DD
However, when I retrieve these values in to my web application, they are displayed in the following format:
YYYY-MM-DD HH:MM (the time is specifically 12:00).
Why does this happen? And how can I prevent this from happening?
when you store in C# your date field, you use DateTime object. In this object when you don't specify the time part will be put a default value depends on Globalization.
You can study how DateTime works here
You can convert the date to the format you like when you fetch the data, using date_format():
select date_format(datecol, '%Y-%m-%d')
This returns the value as a string.
You shouldn't retrieve the value as a string from mysql. Why? Because if you ever need to do any operations on that value, such as adding a day, then you will need to parse it back into a DateTime again. String parsing can be slow, and when it comes to dates they are prone to errors like misinterpretation of mm/dd/yyyy and dd/mm/yyyy formatting.
The problem you have is that .NET does not have just a Date type. It only has a DateTime type. So loading a MySQL DATE type, is going to get a DateTime with the time portion set to midnight.
There's no direct problem with that, except on how are outputting the result. If you just call .ToString() without any parameters, or you implicitly use it as a string, then you are going to get a result with the full date and time. You simply need to provide a parameter to indicate what formatting you want.
Without any parameters, you are getting the General "G" format. This is explained in the documentation here.
In other words:
yourDateTime.ToString() == yourDateTime.ToString("G")
You can read about all of the other formats available, here and here.
In particular, if you just want the date, then you probably want to do this:
yourDateTime.ToString("d")
Based on your comments, you should be doing this instead:
MySQL Query:
SELECT Gbstartdate FROM TblGbDef
C#:
DateTime gb_start_date = (DateTime) datareader[0];
I have a Data Table that has a column 'subscribeDate' that has dates in MM/dd/yyyy hh:mm:ss format. I would like to change all the dates in this column to MM-dd-yyyy hh:mm:ss format. Is there a way that I can do it without running a loop?
I would hope that the values in the DataTable are of type DateTime or DateTimeOffset.
Assuming that's the case, they don't have a format. They're just dates and times. How you convert them to a string is up to you.
If they're not already in an appropriate data type, you should try to change how you obtain the data to start with. Convert it from a string format to a more appropriate data type as early as you possibly can, and only convert it back to a string when you really need to.
You haven't explained how you're displaying the DataTable, but most ways that I'm aware of allow you to specify a format string - that's where you would specify your MM-dd-yyyy HH:mm:ss format. (Note HH rather than hh, to get 24 hours.)
You should be able to do that, if there is not some culture restrictions in your app (I don't know how works your application) with convert method.
Somethign like this:
myTable.Columns["Date"].Convert(
val => DateTime.Parse(val.ToString()).ToString("dd/MMM/yyyy"));
Where convert function you can find in Is it possible to format a date column of a datatable?
My C# application have to read some date from MySQL database. Problem I have is that format of date depends on system localisation settings.
My question is if is possible that I always get date in formats yyyy-MM-dd hh:mm:ss, and yyyy-MM-dd, no matter of localisation settings.
Thank you in advance!
If you are storing the dates as true date or datetime values, your application will get the raw binary data back, and it will not be subject to localization until you create a string representation of the date values. My guess is that you are looking at the values in the debugger or using Console.WriteLine(theValue);, which will use the current locale. Always include the desired format and/or the desired culture when converting non-string values to strings.
If you are storing the dates as strings, you will always have to know exactly what format went into the database.
Assuming the dates are stored as date or datetime: just handle the values as they are, and don't convert them to strings until you need to show them to a user:
DateTime theValue = theReader.GetDateTime(fieldOrdinal);
var theValueAsText = theValue.ToString(CultureInfo.InvariantCulture);
var specificTextRepr = theValue.ToString("yyyy-MM-dd HH:mm:ss");
The theValueAsText variable will be a string representation that is not tied to a specific culture. The specificTextRepr will be your specific text representation.
You shouldn't be reading it back as a string from the database - you haven't shown how you're reading the data, but if you use something to populate a DataTable, or LINQ, or IDataReader.GetDateTime then there's no string formatting involved (assuming it's stored properly in the database, which it looks like it is).
A DateTime value doesn't intrinsically have a format, any more than an int is in decimal or hex - it's how you choose to convert it that matters, and you should almost always avoid doing that formatting unless you really need to.
Since you store the dates in date and date/time specific representations, formatting does not play into it at all (as opposed to some highly discouraged storage schemes when date/time is stored as strings, when formatting does matter, but for a wrong reason).
When you query MySQL from your C# code, you will get the correct dates no matter what your locale is. They will be displayed differently based on the locale, but they will represent the proper date regardless of the locale settings.
You can format the date directly in the query by using
date_format(dob,'%d/%m/%Y')
select date_format(dob,'%d/%m/%Y') dob from student where Id=1
Change
CurrentDate = DateTime.Now.ToString("MMM d, yyyy");
CurrentTime = DateTime.Now.ToString("hh:mm tt");
TO
CurrentDate = DateTime.Now.ToString("MMM d, yyyy",CultureInfo.InvariantCulture);
CurrentTime = DateTime.Now.ToString("hh:mm tt", CultureInfo.InvariantCulture);
Hi i am reading a value from a table using SqlDatareader, problem is it is always formatting the value acording to the machine date format settings.
for example the original date format in the source table is saved as
yyyy/mm/dd
when i use SqlDatareader.GetValue in a machine that has date set as MM/dd/YY
it is atutomatically converted to that format
is there anyway to retrive the date value with the original date formatting intact?
thanks
There is no "original date formatting", dates are kept internally as numbers, not strings.
Within a SQL query, you can choose the output formatting with
CONVERT(varchar(30), theColumn, nnn)
where "nnn" is one of the common date formats listed here: MSDN.
Personally I find that page confusing, so another (more useful) page is here: SQL Server Helper. From that page:
CONVERT(VARCHAR(20), GETDATE(), 100)
will return 'Jan 1 2005 1:29PM'
Probably not what you're looking for. But if you want to store the formatted date time, may be you should rather use VARCHAR or CHAR(N) as the field type. I would think the DATETIME field is to store datetime and the format it self isn't that important (or what it is meant for). However, you could reconvert it back to that format in C#.