ZubinAmit
ZubinAmit

Reputation: 76

Datetime to Date in c# for WSDL

We have a WSDL which is a PHP based webservice.

When the WSDL is included in Visual Studio it Creates its Proxy Classes. Which makes Stuff easy, We just need to Set the Params in the Proxy Classes Properties. And Call the Proxy Methods.

Everthing was Working Smooth, until DOB was needed for a Particular method.

When this Link was opened in browser we found XML format.

<xsd:element name="DOB" type="xsd:date" minOccurs="0"/>

Clearly DOB ask for date datatype. But the Reference.cs holding all the Proxy Components of the WSDL understood it as.

private System.DateTime dOBField;

[System.Xml.Serialization.SoapElementAttribute(DataType="date")]
        public System.DateTime DOB {
            get {
                return this.dOBField;
            }
            set {
                this.dOBField = value;
            }
        }

We did little bit of research on Date for C#. C# is unaware of the Concept of Date. It only understands DateTime.

For Example. if we try parse a string to Date, Lets take todays date 28thJan.

string today = "2015-01-28";
DateTime dtToday = DateTime.ParseExact(today , "yyyy/MM/dd", null).Date;

The Value of dtToday will be 2015-01-28 12:00:00AM. But we just asked for Date.

Now Comes the Real issue Whenever we are trying to Set Date for the DOB proxy property it is type casting it to DateTime something like "2009-02-27 12:12:22PM'

But PHP is expecting for something like '2009-02-27'. We tried Editing the Reference.cs. It threw Exception at time of Proxy Object initialization.

So Please Anybody guide us some way out.

Thanks in advance.

Upvotes: 2

Views: 1585

Answers (2)

Soner G&#246;n&#252;l
Soner G&#246;n&#252;l

Reputation: 98740

The Value of dtToday will be 2015-01-28 12:00:00AM. But we just asked for Date.

A DateTime in .NET Framework always have a date and time part. .Date property just sets it's time part to midnight.

That's why you can't get a 2009-02-27 as a DateTime. This can only be it's textual representation which you can get it usually .ToString() method.

dtToday.ToString("yyyy-MM-dd");

By the way, I have to say, you are so lucky about your parsing operation. You didn't even provide exact match between your string and format, but since you use null as a IFormatProvider, looks like your CurrentCulture has - as a DateSeparator and / automatically replace itself to it. :)

Upvotes: 2

MethodMan
MethodMan

Reputation: 18843

If you want the textual representation as Soner has mentioned you can do the following

DateTime theDate = DateTime.ParseExact("2015-01-28", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
string strTheDate = theDate.ToString("yyyy/MM/dd");

Upvotes: 1

Related Questions