Datetime conversion independent of system date format [duplicate] - c#

I need convert the date of string format 'MM/dd/yyyy' to datetime format 'MM/dd/yyyy' when the system date format is 'dd/MM/yyyy'.

DateTime stores a date as a numerical value, not as a string. So asking for a DateTime type of format 'MM/dd/yyyy' doesn't make much sense. If you are displaying the date in a WPF control, it will default to displaying a string that conforms to the system date format. But that is just a display string that you can manipulate using the format strings alluded to in the links above. Using #Sajeetharan's code will create a DateTime struct with the correct value internally. How you display it is up to you and your string formatting choices.
The same goes for dates stored in a database column of type 'datetime'. The value is stored numerically. Your query editor will display the value likely in the same format as your system settings. If the date is stored as a string, then again, #Sajeetharan's code is the correct way to convert the database string into a DateTime struct.

use DateTime.ParseExact
DateTime.ParseExact("05/02/2014", "dd/MM/yyyy", CultureInfo.InvariantCulture)
.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);

This may help:
string dateString="05/02/2014";
DateTime txtmyDate = DateTime.ParseExact(dateString, "MM/dd/yyyy",
CultureInfo.InvariantCulture);

try this
string dateString="05/02/2014";
DateTime txtmyDate =convert.toDateTime(dateString);

DateTime dt = DateTime.ParseExact("05/02/2014","MM/dd/yyyy",CultureInfo.InvariantCulture);

Related

String with dd/MM/yyyy format to Date time irrespective of System date time format [duplicate]

I need convert the date of string format 'MM/dd/yyyy' to datetime format 'MM/dd/yyyy' when the system date format is 'dd/MM/yyyy'.
DateTime stores a date as a numerical value, not as a string. So asking for a DateTime type of format 'MM/dd/yyyy' doesn't make much sense. If you are displaying the date in a WPF control, it will default to displaying a string that conforms to the system date format. But that is just a display string that you can manipulate using the format strings alluded to in the links above. Using #Sajeetharan's code will create a DateTime struct with the correct value internally. How you display it is up to you and your string formatting choices.
The same goes for dates stored in a database column of type 'datetime'. The value is stored numerically. Your query editor will display the value likely in the same format as your system settings. If the date is stored as a string, then again, #Sajeetharan's code is the correct way to convert the database string into a DateTime struct.
use DateTime.ParseExact
DateTime.ParseExact("05/02/2014", "dd/MM/yyyy", CultureInfo.InvariantCulture)
.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
This may help:
string dateString="05/02/2014";
DateTime txtmyDate = DateTime.ParseExact(dateString, "MM/dd/yyyy",
CultureInfo.InvariantCulture);
try this
string dateString="05/02/2014";
DateTime txtmyDate =convert.toDateTime(dateString);
DateTime dt = DateTime.ParseExact("05/02/2014","MM/dd/yyyy",CultureInfo.InvariantCulture);

Convert date time string in dd/mm/yyyy to datetime format mm/dd/yyyy

There is an input from CSV file which is in dd/mm/yyyy format.
I have to pass these date values to the stored procedure.
I want to convert this to mm/dd/yyyy format before I bulkcopy it to the database table from the datatable in the c# code.
Should I do this in the code or in the stored procedure which I am sending it to?
Please advise.I have tried all possible combinations.
You could you use DateTime.ParseExact,
var parsedDate = DateTime.ParseExact(dateString, "dd/MM/YYYY", CultureInfo.InvariantCulture);
where dateString is the string representation of the date you want to parse.
If your stored procedures expects a DateTime you could use the parseDate value. Otherwise, if it expects a string in the format you mentioned, you can pass the following value:
parsedDate.ToString("MM/dd/YYYY")
You should parse your value to DateTime in C# and pass this date value to your SQL client or ORM without converting it to a string
If your SQL column type is set to either one of the date value types it is quite impossible to format the date according to your desire, since the database engine does not store the formatted value but the date value itself.
Make sure to parse the DateTime in-code before updating its value in the SQL database.
string date = "2000-02-02";
DateTime time = DateTime.Parse(date); // Will throw an exception if the date is invalid.
There's also the TryParse method available for you. It will make sure the date value you're trying to parse is indeed in the right format.
string input = "2000-02-02";
DateTime dateTime;
if (DateTime.TryParse(input, out dateTime))
{
Console.WriteLine(dateTime);
}
After the storage you're more than welcome to select your preferred display format for your DateTime variable using one of the given formats (read link below for a full list of available formats).
https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx

Convert date format of string to datetime MM/dd/yyyy when system date formate is dd/MM/yyyy

I need convert the date of string format 'MM/dd/yyyy' to datetime format 'MM/dd/yyyy' when the system date format is 'dd/MM/yyyy'.
DateTime stores a date as a numerical value, not as a string. So asking for a DateTime type of format 'MM/dd/yyyy' doesn't make much sense. If you are displaying the date in a WPF control, it will default to displaying a string that conforms to the system date format. But that is just a display string that you can manipulate using the format strings alluded to in the links above. Using #Sajeetharan's code will create a DateTime struct with the correct value internally. How you display it is up to you and your string formatting choices.
The same goes for dates stored in a database column of type 'datetime'. The value is stored numerically. Your query editor will display the value likely in the same format as your system settings. If the date is stored as a string, then again, #Sajeetharan's code is the correct way to convert the database string into a DateTime struct.
use DateTime.ParseExact
DateTime.ParseExact("05/02/2014", "dd/MM/yyyy", CultureInfo.InvariantCulture)
.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
This may help:
string dateString="05/02/2014";
DateTime txtmyDate = DateTime.ParseExact(dateString, "MM/dd/yyyy",
CultureInfo.InvariantCulture);
try this
string dateString="05/02/2014";
DateTime txtmyDate =convert.toDateTime(dateString);
DateTime dt = DateTime.ParseExact("05/02/2014","MM/dd/yyyy",CultureInfo.InvariantCulture);

remove time from date and print only date in datetime format

i have date stored in a string format as follows: "2014-03-12"
im passing this to a database which accepts the date as datetime.
im converting the string date to datetime format as follows:
DateTime StartDates = Convert.ToDateTime(StartDate);
but the time gets appended along with the date as "2014-03-12 12:00:00:00"
can anyone tel me how to send only the date leaving out the time.
i want the final date to be still in datetime format only but with time part cut off
DateTime is irrespective of the format. Formatting is only useful for presentation purpose. A DateTime object will have a Date part and Time part. When you try parsing your string "2014-03-12", it doesn't have a Time part, so in the parsed object, Time is set to 00:00:00.
If you just want to to display date then you can use DateTime.ToShortDateString method or use a custom format like:
string formattedDateString = StartDates.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
If you're happy with the date part, you may simply return the Date property of the DateTime conversion result:
DateTime StartDates = Convert.ToDateTime(StartDate).Date;
I should mention that using Convert.ToDateTime puts you at the mercy of the process current culture date format though, so you probably want to use the ParseExact or ToString method like the other answers suggests, with the appropriate format and culture instance.

C# datetime format change

I want to convert datetime.now format which is "dd/mm/yyyy hh:mm:ss AM/PM" to US time format i.e. "mm-dd-yyyy hh:mm:ss AM/PM". More over i want the converted format as datetime not in string because the same is being stored in the database and the field in the database is in datetime format so it will not take string.Please suggest....
A DateTime value doesn't have a format. DateTime.Now doesn't have a format, and if you do any kind of conversion, if you've got a DateTime, there's no format. The concept of a format only applies when you're converting to or from a string - and DateTime.Now is a DateTime, not a string.
Just insert the value into the database using DateTime.Now (or DateTime.UtcNow) and you'll preserve the data which is the important part.
You should only perform string conversions when you actually have to - at which point you can use DateTime.TryParseExact and DateTime.ToString using custom date/time format strings. (Use TryParseExact for user input; for machine input which really should be valid, use DateTime.ParseExact.)
Note that it's usually a good idea to use custom date/time patterns with the invariant culture, or standard date/time patterns with "real" cultures (e.g. the user's culture).
Try this. Standard Date and Time Format Strings
DateTime.Now.ToString("g",CultureInfo.CreateSpecificCulture("en-us"))
DateTime usDateTimeFormat = DateTime.Parse(DateTime.Now ,
System.Globalization.CultureInfo.GetCultureInfo("en-us"));

Categories