I need have this format :
aaaa-mm-jjThh:mm:sszzzzzz
And put it in a XML property "DateTime" type.
So, I did it :
var xmlObj= new xmlObj.tHeader();
xmlObj.prop = DateTime.ParseExact(DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"), "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'", CultureInfo.InvariantCulture);
Console.WriteLine(xmlObj.prop);
The Console.WriteLine instruction return : 03/05/2016 15:43:10
I don't understand why the property remove the format.
In the XSD, this property is waiting a datetime format.
Any ideas?
EDIT :
Ok, on the command result, I see a default format but If convert my XML object to File, the format is correct :
Very strange... but it's ok now. Many thanks to all of you
You are calling DateTime.ParseExact, which parses a string into a DateTime object. Just drop that part and assign the result of ToString directly to xmlObj.prop, or assign the DateTime object directly (if that's what it's looking for).
Edit:
To address your edit, your XML file is generated correctly. However, when you output the DateTime prop to the console, it uses the default string format for a date. You can format that with ToString() if you want to.
You try this:
Console.WriteLine(DateTime.UtcNow.ToString("s") + "Z");
Your format is wrong in parse, if you want yyyy-MM-ddTHH:mm:ssz format then you have to convert it as string otherwise, it will represent standard datetime, try this
xmlObj.prop = DateTime.ParseExact(DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ssZ"), "yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture);
Console.WriteLine(xmlObj.prop.ToString("yyyy-MM-ddTHH:mm:ssz"));
Related
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.
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");
Here is my problem:
I have a XElement storing a DateTime, and I want to get the Value property returning the DateTime in a custom format.
I was wondering if there is a way to include a IFormatProvider in the XElement.
I think you are mixing things here. The XElement always stores its value as a string. To get a custom date format, you need to convert this string into a DateTime object first and then call ToString with the format you want to have.
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.
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.