调用Web服务时发生日期时间问题 [英] Datetime issue while calling Web Service

查看:73
本文介绍了调用Web服务时发生日期时间问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调用Web服务时,我必须以'yyyy-MM-ddTHH:mm:ss'的格式发送当前日期时间.通过使用ToString()方法,我可以获得任何所需的格式,但是我的服务请求对象期望的是datetime对象而不是字符串对象. 我尝试为当前线程的DateTimeFormatInfo设置所需的格式,如下所示,

While calling a web service i have to send the current datetime in the format of 'yyyy-MM-ddTHH:mm:ss' . By using ToString() method i can get any required format, but my service request object expecting the datetime object not the string object. I tried setting the required format for the DateTimeFormatInfo of the current Thread as shown below,

CultureInfo culture = (CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
DateTimeFormatInfo dateTimeFormat = culture.DateTimeFormat;
dateTimeFormat.ShortDatePattern = "yyyy-MM-ddTHH:mm:ss";
dateTimeFormat.LongTimePattern = "";
System.Threading.Thread.CurrentThread.CurrentCulture = culture;
DateTime dtweb = System.DateTime.Now;

但是它会根据ShortDatePattern或LongTimepattern设置在日期时间对象的开始或结束处提供一个空格.

But its giving one Whitespace either at starting or end of the datetime object based on the the ShortDatePattern or LongTimepattern settings.

还有其他方法可以获取我所需的格式吗?

Is there any other way i can get my required format?

推荐答案

如果您尝试这样做,您将实现自己的目标期待吗?

And if you try that, you will achieve what you expecting?

            if (DateTime.TryParseExact(DateTime.Now.ToString(), "yyyy-MM-ddTHH:mm:ss", null, System.Globalization.DateTimeStyles.None, out dt))
            {
                Console.WriteLine(dt);
            }


这篇关于调用Web服务时发生日期时间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆