生成带有日期值的字符串 [英] Generate String with date values

查看:62
本文介绍了生成带有日期值的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我需要一个函数,该函数生成具有从date/到date的日期值的字符串.
字符串必须如下所示(格式:yyyy-MM-ddTHH:mm:ss.fff):

Hi,
I need a function which generates a string with date values from date / to date.
The string must look as follows (format: yyyy-MM-ddTHH: mm: ss.fff):

" N'2015-01-01T00:00:00.000',N'2015-01-02T00:00:00.000',N'2015-01-03T00:00:00.000',..." usw

谢谢
LG
妮可

"N'2015-01-01T00:00:00.000', N'2015-01-02T00:00:00.000', N'2015-01-03T00:00:00.000', ..." usw

Merci
LG
Nicole

推荐答案

该格式为RFC1123格式.您可以使用ToString("r")在任何日期时间生成该值.

That format is the RFC1123 format. You can generate that against any datetime using ToString("r").

var str = DateTime.Now.ToString("r");

前面的N东西看起来像SQL中的Unicode字符串,而不是实际DT字符串的一部分.但是,如果您真的想要它,则可以添加它.要生成多个值,只需使用String.Join.

The N thing on the front looks like a Unicode string in SQL and isn't part of the actual DT string. But if you really wanted it then you could add it. To generate multiple values simply use String.Join.

var dates = new [] {
   new DateTime(2015, 1, 1),
   new DateTime(2015, 1, 2),
   new DateTime(2015, 1, 3)
}

//Format as RFC1123 with an N in the front...
var strings = dates.Select(d =>


"N'{d.ToString(" r)}'");; //以逗号分隔 var final = String.Join(,",字符串);
"N'{d.ToString("r")}'"); //Join them separated by commas var final = String.Join(",", strings);

您当然可以根据需要将它们组合在一起.

You can, of course, combine these as needed.

迈克尔·泰勒
http://www.michaeltaylorp3.net

Michael Taylor
http://www.michaeltaylorp3.net


这篇关于生成带有日期值的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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