Change format of Date? - c#

I'm currently getting the date from a standard month calendar and displaying it in the dd-mm-yyyy format. What I would like to do is make it so that when the date is grabbed along with the display it will also store the date in the format yyyy-mm-dd without the hyphens being present so for example today would be 20160511 and I was wondering how I could do this?

Are you using DateTime? Then you could do:
date.ToString("yyyyMMdd");

string date = DateTime.Today.ToString("yyyyMMdd");

Related

Unable to parse a string to date when date format is unknown in C#

I want to know if a particular string is a valid date or not while the date format can be any possible one.
(NOTE- date format is unknown).
Currently I am using
DateTime.TryParse, but its not working with all the dates as it is not parsing this date string=> 5-27-58.
Cant use DateTime.ParseExact it requires a specific date formates.
what is the best approach to do this.

Is there a way to format a date time object?

So last week, the way our application worked was that the back-end team was storing dates as a string in YYYY-MM-DD formats. This week, they changed it to be a DateTime object instead of a string. So now I'm just creating a DateTime object from the string value on this particular DateTime control we use.
So basically with our custom control , it was like this:
mySearchModelObject.fromDate = myDateRangeControl.Values[0]; //string
mySearchModelObject.toDate = myDateRangeControl.Values[1]; //string
Now it's more like this:
DateTime fromDate, toDate;
DateTime.Tryparse(myDateRangeControl.Values[0], out fromDate);
DateTime.Tryparse(myDateRangeControl.Values[1], out toDate);
mySearchModelObject.fromDate = fromDate;
mySearchModelObject.toDate = toDate;
But searching with the same date range as last week yields different results from the DB.
I'm wondering if it's because our dates were "YYYY-MM-DD" as strings, but now it's getting a date time object in whatever the system's format is + the time itself.
So is there a way to format my DateTime object to still have it in the same YYYY-MM-DD format?
Use DateTime.TryParseExact and provide "yyyy-MM-dd" as format string.
See http://msdn.microsoft.com/en-us/library/h9b85w22
you can do fromDate.ToString("yyyy-MM-dd") to have a date formatted as YYYY-MM-DD
see msdn on standard and custom datetime format
see demo https://dotnetfiddle.net/NZz0HG
also if mySearchModelObject.fromDate is of type object, you can assign a DateTime or a string, no compiler warnings/error.
But when is used, maybe with mySearchModelObject.fromDate.ToString() you get a different result, before was '2014-12-31' and now 12/31/2014 12:00:00 AM

How do I display a date format in dd/MM/yyyy to ddd, dd/MM/yyyy (Mon, 1/4/2014) format with this logic?

Right now I have been given the code below code to get the selected date from a calendar and displays it into a textbox but it's in dd/MM/yyyy format.
txtSMAlerted_PopupControlExtender.Commit(calSMAlerted.SelectedDate.ToShortDateString());
It is simply a format string passed to the ToString method of the DateTime
calSMAlerted.SelectedDate.ToString("ddd, dd/MM/yyyy");
The ToString(string) overload of DateTime accepts a Standard Date Format String as well as a Custom Date And Time Format String.
With this last set of rules it is easy to build your required output format.
Try this:
calSMAlerted.SelectedDate.ToString("ddd, dd/MM/yyyy")
you should parse datetime into string like this:
calSMAlerted.SelectedDate.ToString("ddd, d/M/yyyy");

Datetime Conversion in C#

I receiving dates in the USA standard format mm/dd/yy.
I can upload to MySQL only in the format of yyyy-MM-dd.
How can you convert mm/dd/yy format to yyyy-MM-dd format in C#?
You could parse it and then format it. (DateTime.ParseExact, DateTime.ToString)
However, you shouldn't have to reformat for MySQL's benefit, and it suggests your database access is inappropriate:
Your database column should be a DATETIME column or something similar, if you're storing dates in it
Your database code shouldn't be including the value as a string at all. It should be passing it as a parameter with a DateTime value
So using parameterized SQL, you should just need to parse (e.g. using DateTime.ParseExact), then pass it up as a DateTime:
DateTime date = DateTime.ParseExact(text, "mm/dd/yy",
CultureInfo.InvariantCulture);
command.Parameters.Add("#Foo", MySqlDbType.DateTime).Value = date;
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx ... please check here. you can convert the date format in C#
You can use MySQLs STR_TO_DATE(str,format) function. Or you can use string formatting in c#
Something like this should work:
DateTime dt = Convert.ToDateTime(dateString);
string newDateString = dt.ToString("yyyy-MM-dd");

How to convert MM/dd/yyyy to yyyy/MM/dd in C# using globalization

I have a text box in which a user is supposed to enter a date in MM/dd/yyyy format. This date is stored as yyyy/MM/dd in the database.
I want the user to enter the date in MM/dd/yyyy format and later I want to convert it to yyyy/mm/dd so that I can query the database.
How can I convert the user input date MM/dd/yyyy to yyyy/mm/dd?
If you're certain of the input string's format, use DateTime.ParseExact specifying "MM/dd/yyyy", then return the DateTime using .ToString with the appropriate "yyyy/MM/dd" format string.
There's no need to reference anything in the System.Globalization namespace for this.
That said, your database should be storing dates with a datetime format, rather than a string, in which case the format doesn't matter as your DBMS should do the conversion for you.
You can parse the date and format the result:
string str = Date.Parse(myDate).ToString("yyyy/MM/dd");
Alternatively, if the current culture doesn't support that date format and you've already validated the input:
string items[] = myDate.Split('/');
string str = items[2] + "/" + items[0] + "/" + items[1];
When you said globalization, I assume you want the change to be automatic according to current culture
You can setup culture (at Global.asax.cs I suggest)
System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("the culture you want to set"); //
you do not need to touch any datetime thing, it just happened when you output them.
One thing is not the other.
Not sure why you want to insist on the text entered being MM/dd/yyyy, or why you haven't used a date time picker to make sure it is a date.
But at the point you get the content of the textbox as a date, parse it based on globalisation, or a set of acceptable formats. Now it is a date, and assuming it's a date in the database, format is irrelevant until you come to populate the text box, with some content from the DB, inwhich case you use DateTime's ToString method witha globalisation parameter, usually CultureInfo.CurrentCulture, or if you've got away with it CultureInfo.InvariantCulture.
If it's a string in the DB, then this is the least of your problems.
They key point is if you use dates properly, format is only relevant for Parse, and ToString type methods.
i am using
IFormatProvider culture=new CultureInfo("en-GB",true);
sqlcommand cmd=new sqlcommand("query",con);
cmd.Parameters.AddWithValue("#date",DateTime.Parse(txtdate.Text.Trim(),culture,DateTimeStyles.NoCurrentDateDefault).Date);
for this to work the format property must be set to dd/MM/yyyy
and text box read only property must be false

Categories

Resources