Converting the string to datetime using asp.net - c#

string Edate = collection["effectiveDatePicker"].ToString();
string Cdate = collection["cancelDatePicker"].ToString();
The Date I am getting is 20101112 or 20101116
Then I am doign soemthign like this to assing my Datetime variable
h.Edate= DateTime.ParseExact(collection["effectiveDatePicker"].ToString(), "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture, DateTimeStyles.NoCurrentDateDefault);
h.CDate= DateTime.ParseExact(collection["cancelDatePicker"].ToString(), "yyyyMMdd", System.Globalization.CultureInfo.CurrentCult
After saving to the data base.. I am seeing the EDate and CDate fileds somethign like this
11/10/2010
11/15/2010
with the same dates when I Submit again I am getting Error in the ParseExact bec the string I am getting is 11/10/2010 but its expecting 20101010
Can any body help me out?

You can specify the format that your dates are coming in as like this:
string Edate = collection["effectiveDatePicker"].ToString("yyyyMMdd");
string Cdate = collection["cancelDatePicker"].ToString("yyyyMMdd");
This should ensure that you are working with strings that look like what you want.

I can tell you for sure that there is a reformatting problem. I don't fully understand your code and the steps it takes (I guess you are using data binding or something similar).
The point seems that dates are initially set as yyyyMMdd, but when you postback the ToString() operator is applied to these dates, converting them to your OS's native format MM/dd/yyyy.
You must force them to be converted into yyyyMMdd again, because by default ToString will use CurrentUICulture which is not good for you.
You should show us the update code

I'm not sure what your specific question is. Because you're using DateTime.ParseExact() and are specifying a format of 'yyyyMMdd', passing in a string such as '11/04/2010' will fail.

Related

Removing time part from a string c#

I am trying to remove the time zone from a string.
The string looks like below.
2/05/2018 6:54:00 AM
This data has been retrieved from a local database.
Code Part:
string datCommisioned = (rdr.GetValue(4).ToString());
string dateonly = datCommisioned.ToString("dd/MM/yyyy");
When I tried to execute the above coding it's getting an error,
cannot convert from 'string' to 'System.IFormatProvider'
Any idea how to catch the date part excluding the time? Thanks!
Edit: This is probably not a duplicate question as I refer the other questions before posting. The context of this question is different because the string I'm extracting is not recognised as a valid DateTime (Manually entered by users) thus DateTime class cannot be used.
//Assuming value at index 4 is a DateTime.
string datCommisioned = rdr.GetDateTime(4).ToString(#"dd/MM/yyyy");
It's good practice to use the more specific and strongly typed methods with a DataReader. Helps identify bugs faster.
There are several approaches
Use DateTime object.
DateTime dateTime = DateTime.Parse(datCommissioned);
string dateonly = dateTime.ToString("dd/MM/yyyy");
Split on whitespace
var split = datCommissioned.Split(' ');
string dateonly = split[0];
You just need to change it to be this.
var datCommisioned = DateTime.parse(rdr.GetValue(4)).ToString("dd/MM/yyyy");
You are currently converting it to a string then trying to ToString() the String, when you just need to toString() the date from the reader.
Just another approach here; You can utilize DateTime.Parse() or perhaps a safer alternative, DateTime.TryParse(), to convert the string to a DateTime object. Then, use the Date property of the DateTime object to basically set the time to midnight, ie. 00:00:00. Depending on what you’re using the date for, having the DateTime object instead of a string could be helpful. You can then call ToString(“d”) or use another formatter to give you the date only portion in the desired format.
if (TryParse(datCommissioned, out DateTime commissionedDate))
{
commisionedDate = commisionedDate.Date;
string commissionedDateText = commissionedDate.ToString(“dd/MM/yyyy”);
}

C# DateTime to be copied without format getting changed

I have a date and time which should be copied to DateTime object without changing its format.
Is there a way to resolve it?
Pls see the code below
string dateTime = "07/20/11 14:40:28";
DateTime copyDateTime = Convert.ToDateTime(dateTime);
string dateTime2 = copyDateTime.ToString();
Output:
{7/20/2011 2:40:28 PM}
If you notice the output, it got changed to PM. I want it as it is. How to get it?
EDIT:
I want dateTime2 to have the value exactly as it was for dateTime.
Format is not intrinsically associated with the DateTime. Format is simply a display property.
If you need to display it in your preferred format than simply call:
Console.WriteLine(copyDateTime.ToString("G"));
See MSDN for a complete list of standard format strings.
Before outputting, you need to convert the DateTime back into a string. By default, it simply calls "ToString" which uses the default DateTime format configured for the current user/locale.
Use ToString and specify a format to convert the datetime back into a String, then you can control the format.

Can't convert DateTime format from MM/dd/yyyy to yyyy-MM-dd

I know this is a recurrent question but I cannot seem to be able to get this to work even after a lot of research! I want to convert a string to a DateTime.
public DateTime ConvertToDateTime(string thisDate)
{
return DateTime.ParseExact(thisDate, "yyyy-MM-dd", CultureInfo.InvariantCulture);
}
(EDITED)
I give "2012-03-07" as a parameter and I get a DateTime that is 03/07/2012 12:00AM. When I wanted it to return: 2012-03-07 00:00:00
Your code is correct; your expectations are wrong.
When a method returns a DateTime, it has no format. Well, more accurately, it has a binary in-memory format that has no relation to its string format.
The debugger formats the DateTime to show it to you, but that has no bearing on the DateTime value itself. The format it uses is presumably determined by your locale settings.
When you want to display the DateTime to a user, or pass it as a string to some other function, you can format it as you like using one of the ToString overloads.
It may be that you are displaying or passing the return value of the function you've given; if so, the error is in the code displaying or passing the DateTime, not in the function you've given. If that's the case, edit your answer to include that code, and perhaps we can help you fix it.
Try this
public String ConvertToDateTime(string thisDate)
{
return DateTime.ParseExact(thisDate, "yyyy-MM-dd",
CultureInfo.InvariantCulture).toString("yyyy-MM-dd");
}

Retrieve format of date from date value

I am receiving some data into a variable of type object. In certain cases this data are date values. For that data, I would like to convert this to a string and return it in the same format as it was passed. In some cases, the object could be a datetime, in others a date only or time only values.
As soon as I convert the object to a date or a string, it is obviously given a time of midnight which in my scenario may be a valid time (so I cannot test to see if the time is midnight in which case I could deduce that it would have been a date only date value, nor can I use regex on it as there will always be a time element).
Intellisense shows me it correctly, ie in the format I am wishing to return the value.
Is there an easy way to achieve this (hopefully without using reflection)
Many thx
Simon
Your question is a little unclear but I think you're looking for something like this:
DateTime result;
if (DateTime.TryParse(value, out result))
{
// use result here
}
In the above code value is a string that represents the data coming in. The code will only enter the if block if the string is a valid DateTime. At which point you can do the processing you need on it.
Im not sure i understand the question but i would recommend you to take a look at this conversion example on MSDN, and see the Documentation of the DateTime Structur it contains a lot of Conversion/Formatting Methods i hope it helps.
There are many way to do formatting on the datetime and one of the simple way is fetch the data from the required table in the desired format. Like here you need to display the date and if you your format is dd/MM/yyyy then try this
select Convert(varchar(10),StartDate,103) as StartDateformat from table where filtername=#filtername
use this link to find other format Cast and Convert
From local variable to DateTime Conversion
DateTime todateformat = Convert.ToDateTime(txttodate.Text.Trim());
From DateTime to local variable Conversion in specific format
string startdate = todateformat.ToString("dd/MM/yyyy hh:mm:ss");

.NET DateTime to BizTalk DateTime

I have date in this format "1999-05-31T13:20:00.000-05:00" I want to add some hours or days to it . BizTalk expects same datetime format how can i pass it as Datetime in that format ? No string . Date time with same format as source date.
You should be able to do something like this to get it into a DateTimeOffset object. After that you can call whatever methods you want on it.
DateTimeOffset dateTime = DateTimeOffset.Parse( "1999-05-31T13:20:00.000-05:00" );
To get the value back just use a formatting string.
dateTime.ToString( "O" ); //this should be the same format as you started with
Here are some other options http://msdn.microsoft.com/en-us/library/az4se3k1.aspx
Here is a link to the DateTimeOffset structure
http://msdn.microsoft.com/en-us/library/bb351654.aspx
I hope this helps.
The DateTime object is format-independent (for the most part). So whether or not it starts in the format you list or not doesn't matter. You can always get it back into that format (using the ToString("o") function). But that's as a string (when format matters).
After a quick search, it looks like you must be talking about string format, even though you said no string. So the other answer or the ToString("o"); part of mine is relevant.

Categories

Resources