Changing In DateTime Format [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have changed format to dd-mm-yy in backend of page, i want to save date in'dd-mm-yy' format e.g.'23-02-2015' in database.and retreieve it in this format, i am using datetime picker in the text field...i changed the format of date picker to my 'dd-mm-yyyy' but its not saving data in that format and cause the below error
String was not recognized as a valid DateTime.
Any suggestions?

Simply don't use strings to represent dates.
the DateTime struct of c# maps to sql server's datetime data type directly, meaning that you can simply send the DateTime struct as a parameter value in your SqlCommand object (I hope you are using parameterized queries or stored procedure. If not, then start using them right now, unless you want to be vulnerable to Sql injection attacks.)
Read here and there about the difference between Display format and storing format of date values in sql server.

"String was not recognized as a valid DateTime."
First , why aren't you sending datetime object ?
If you can't(!) , well ,
If your datetime object id dt so do dt.ToString("yyyy-MM-ddTHH':'mm':'sszzz", DateTimeFormatInfo.InvariantInfo)
This will be a valid iso datetime string.
The only truly safe formats for date/time literals in SQL Server, at least for DATETIME and SMALLDATETIME, are:
YYYYMMDD
YYYY-MM-DDThh:nn[:ss[:mmm]]

Related

How to convert date format with T format in c# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a date format as shown below 2021-02-11T13:00:00+04:00 which i need to get in following format 11-FEB-20 01.00.00.000000000 PM.How can i achieve it ?
Parse and then format according to Custom date and time format strings
DateTime dt = DateTime.Parse("2021-02-11T13:00:00+04:00");
Console.WriteLine(dt.ToString("dd-MMM-yy"));
You can Parse form existing format into DateTime and then format it into String with the desired format string:
string source = "2021-02-11T13:00:00+04:00";
string result = DateTime
.Parse(source)
.ToString("dd-MMM-yy hh.mm.ss.fffffff'00' tt", CultureInfo.InvariantCulture)
.ToUpper();
Please, note
ToUpper() since we want FEB, not Feb.
fffffff'00' - we can't provide 9, but 7 digits after the decimal point, so we have to append two zeros.

How to get the date from the given string in c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I only need date from this string Wed, 02/05/2020 - 12:31 what format should I use whenever I am using
{MM/dd/yyyy} I am getting the same value if there is any other way please let me know
item.changed=Wed, 02/05/2020 - 12:31
`if (ListRecord.checkresponse == "Response Letters")
{
string checkresponse = item.changed;
string issueDate = string.Format("{0:MM/dd/yyyy}", checkresponse);
}`
To use date formatting, you need to start with a DateTime object, not a string.
So in your scenario you'll have to parse your string as a DateTime, then format it out again to a different string format. There's no way to directly format string -> string, all the logic about formatting is in the DateTime class.
Here's an example:
string changed = "Wed, 02/05/2020 - 12:31";
var checkresponse = DateTime.ParseExact(changed, "ddd, MM/dd/yyyy - HH:mm", CultureInfo.InvariantCulture);
string issueDate = string.Format("{0:MM/dd/yyyy}", checkresponse);
Console.WriteLine(issueDate);
Demo: https://dotnetfiddle.net/bhNImo
(Alternatively, since what you mainly want to do here is strip the first and last parts of the string and keep the date part, you could use a regular expression do to this from the string, but overall it's probably more reliably to use date parsing and formatting.)

Why does DateTime.Parse() get wrong year when parsing dd-MMM date? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I created an application in which the user enter a month-year dd-MMM date as input, and this date is parsed using the DateTime.Parse() method. It is working fine on my PC.
Dim txtDate as String = combobox1.Text
Dim dtDate as DateTime = DateTime.Parse(txtDate)
Example:
DateTime.Parse("01-Dec")
Result:
2017-12-01 00:00:00
But when running this application on other machines, it returns 1899 year.
1899-12-01 00:00:00
After searching I found a similar question here: Time parsing Issue using DateTime.ParseExact() and it is marked as answered, but there is no solution for this situation
Does anyone knows what causes this problem?
Note: it is an old application I am working on. I know that it is recommended to replace it with a DateTime picker, but the issue is very confusing
Date.Parse without specifying an IFormatProvider will use the current user's Culture settings to determine what formats to parse, so 01/04/2017 will be 2017-April-01 on most computers (dd/MM/yyyy) but 2017-Jan-04 on computers in the US (MM/dd/yyyy).
If you're using the same format everywhere, you should use DateTime.ParseExact and provide an explicit format.
As for your specific problem, the string 01-Dec does not specify a year component, the computer must therefore infer the date, and how that date is inferred is often down to the Culture setting too.

Datetime Format to Date [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to convert this?
public DateTime Date { get; set; }
I need to display only date.
Please could any one help to convert this to Date only
By 'display' I assume you mean to the console. Based on this assumption:
Console.WriteLine(Date.Date);
or
Console.WriteLine(Date.ToString("MM/dd/yyyy")); (as an example; there are many other strings you can pass into this overload).
More on the available strings here:
http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
I need to display only date.
The DateTime structure has numerous ways to accomplish this, most commonly .ToShortDateString() and .ToLongDateString():
var displayDate = Date.ToShortDateString();
For further customizations, you can supply a formatting string to the .ToString() method.
You can use the DateTime format string
eg,
Console.WriteLine(Date.ToShortDateString());
Console.WriteLine(Date.ToString("d"));
Console.WriteLine(Date.ToString("yyyy/MM/dd"));
See,
DateTime Format Methods - http://msdn.microsoft.com/enus/library/system.datetime_methods(v=vs.110).aspx
Standard DateTime Format Strings -
http://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx
Custom DateTime Format Strings -
http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
A DateTime always contains the Date and the Time - but you can just output the Datepart of it by using .ToString("d") as mentioned here: Extract the date part from datetime
This will get the date with the time being 00:00:00
public DateTime date { get { return this.date; } set; }
but as stated in a previous answer to display a datetime with only the date you should check out datetime.tostring formats.
http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx

override default DateTime format in C# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I am working in an application built in .NET framework 2.0 but I am running it in .NET framework 4.0, with SQL server as a database.
when I try to take date as a input from Calendar controller (Webforms) its in the format dd/MM/yyyy and this application using Convert.ToDateTime() to parse this date and it throws exception.
so how can I resolve this problem and is there any way that DateTime class object store the date in specific format means I want to store date in dd/MM/yyyy format even when I copy this date object to another, then both object should have same format.
and sorry for My English :p
Assuming you mean this Calendar control, you should just use the SelectedDate or SelectedDates property - let the control handle the conversions.
In general, you should avoid performing textual conversions unless you really need to. (This includes when you interact with the database - use parameterized SQL, and simply pass the DateTime values in the parameters.)
var result = DateTime.ParseExact("29/01/2014", "dd/MM/yyyy", CultureInfo.InvariantCulture);
You should avoid converting DateTime to string and back if you can help it.

Categories