DatePicker/TimePicker format in WP7 - c#

I have nice UI with DatePicker and TimePicker but I need to change the final look of variables which they give.
Date looks like that: 28/02/2012
Time looks like that: 09:18
I would like to have it in SQL DateTime format, so I have two questions:
1) How to change separators in date?
2) How to convert it into SQL format: 2012-02-28 09:18
The only idea what I have in my mind now is to take each part (year, month..etc.) and manually make new datetime variable. Also here I have question. If I will have good format, can I send it to database as a string?

You can set the System.Threading.Thread.CurrentUICulture and/or System.Threading.Thread.CurrentCulture to a custom Culture instance that returns the date & time in the format you require!

Related

Displaying a date Short Format

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"));

Parse date when format is not constant c#

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.

Date type in MySql

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];

Convert DateTime to string with format YYYYMMDD

I have birth dates stored as datetime in SQL Server 2008 like so:
2010-04-25 00:00:00.000
What is the best way, using C#, to convert and format this into a string with a YYYYMMDD format?
In the end, all I need is a string like:
20100425
Any help is greatly appreciated!
date.ToString("yyyyMMdd");
Should be what you need.
You need to get that value into a DateTime object and then you can use it's ToString() function like so:
.ToString("yyyyMMdd");
Are you able to get the data out of the database as a DateTime (.NET) object? If so, you can use the DateTime's instancename.ToString("yyyyMMdd")
If you haven't gotten to that stage yet, there's quite a few different ways to get the data out. It's a whole Google search in itself...
You just format the date using a custom format string:
string formatted = theDate.ToString("yyyyMMdd");
Note that the date doens't have a format at all when it's stored as a datetime in the database. It's just a point in time, it doesn't have a specific text representation until it's specifically created from the date.
Use the .ToString() method on the date time object, and pass in the format you want.

Storing a short date in a DateTime object

I'm trying to store a shortened date (mm/dd/yyyy) into a DateTime object. The following code below is what I am currently trying to do; this includes the time (12:00:00 AM) which I do not want :(
DateTime goodDateHolder = Convert.ToDateTime(DateTime.Now.ToShortDateString());
Result will be 10/19/2009 12:00:00 AM
DateTime is an integer interpreted to represent both parts of DateTime (ie: date and time). You will always have both date and time in DateTime. Sorry, there's nothing you can do about it.
You can use .Date to get the date part. In these cases, the time will always be 12:00 but you can just ignore that part if you don't want it.
You only have two options in this situation.
1) Ignore the time part of the value.
2) Create a wrapper class.
Personally, I am inclined to use option 1.
A DateTime will always have a time component - even if it is 12:00:00 AM. You just need to format the DateTime when you display it (e.g. goodDateHolder.ToShortDateString()).
Instead of .Now you can use .Today which will not remove the time part, but will only fill the date part and leave time to the default value.
Later on, as others pointed out, you should try to get the date part ignoring the time part, depending on the situation.
You'll always get the time portion in a DateTime type.
DateTime goodDateHolder = Convert.ToDateTime(DateTime.Now.ToShortDateString());
will give you today's date but will always show the time to be midnight.
If you're worried about formatting then you would try something like this
goodDateHolder.ToString("mm/dd/yyyy")
to get the date in the format that you want.
This is a good resource msdn-dateformat
You can also check out Noda Time based off the Java Joda Time library.
DateTime object stores both the date and the time. To display only the date, you would use the DateTime.ToString(string) method.
DateTime goodDateHolder = DateTime.Now;
// outputs 10/19/2009
Console.WriteLine(goodDateHolder.ToString("MM/dd/yyyy"));
For more information on the ToString method, follow this link
You might not be able to get it as a DateTime object...but when you want to display it you can format it in the way you want by doing something like.
myDateTime.ToString("M/d/yyyy") which gives 10/19/2009 for your example.
DateTime is merely a UInt64 with useful and clever formatting wrapped around it to make it appear like a date plus a time. You cannot eliminate the time element.

Categories