开发者

Using DateTime object as DataMember?

I need to send in my web service some information about the time + date . So i want to use the DateTime.

Can i define the DateTime as DataMember ? I try to define it开发者_JS百科 as as datamember - but i got an exception ( catastrophic failure )


Create new WCFDate class, and this class outputs a string in a set format, this allows it to be easily read by what ever needs to. Then replace all of Ur DateTimes with WCFDate.

public class WCFDate
{
public static string DateTimeFormat = "yyyy-MM-dd hh:mm:ss zz";

public string Data { get; set; }

public WCFDate() { }

public WCFDate(string data)
{
    Data = data;
}

public WCFDate(DateTime date)
{
    Data = date.ToString(DateTimeFormat);
}

public WCFDate(DateTime? date)
{
    if (date.HasValue)
    {
        Data = date.Value.ToString(DateTimeFormat);
    }
}

public bool HasDate
{
    get
    {
        return !string.IsNullOrWhiteSpace(Data);
    }
}

public DateTime GetDate()
{
    try
    {
        return DateTime.ParseExact(Data, DateTimeFormat, CultureInfo.CurrentCulture);
    }
    catch
    {
        return new DateTime();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜