[英]Assign value of a string (containing date and time) to two different variable (one for Date and one for Time)
I have a string ("CompletionDate") which contains the value "2/28/2017 5:24:00 PM" Now I have 2 variables (EDate and ETime). 我有一个字符串(“ CompletionDate”),其中包含值“ 2/28/2017 5:24:00 PM”现在我有2个变量(EDate和ETime)。 I want to assign the Date to EDate (ie 2/28/2017) and Time to ETime (ie 5:24:00 PM). 我想将日期分配给EDate(即2/28/2017),将时间分配给ETime(即5:24:00 PM)。 How can I split the Date and Time from a single string. 如何从单个字符串拆分日期和时间。 Kindly Help. 请帮助。 My approach right now is like : 我现在的方法是:
string CompletionDate = string.Empty;
string ProjectEDate = string.Empty;
string ProjectETime = string.Empty;
CompletionDate = "2017-03-29 12:58:00";
DateTime dt = DateTime.ParseExact(CompletionDate, "yyyy-MM-dd", CultureInfo.CreateSpecificCulture("en-us"));
DateTime dt1 = DateTime.ParseExact(CompletionDate, "HH:mm:ss", CultureInfo.CreateSpecificCulture("en-us"));
var ProjectEDate = dt.ToString();
var ProjectETime = dt1.ToString();
But its throwing exception that string is not in correct format. 但是它抛出的异常是字符串格式不正确。 Kindly help 请帮助
@Chris pointed one of your problems, but you have one more. @Chris指出了您的问题之一,但还有一个问题。 You are passing full date time string and trying to treat it as date or time only, which is not true. 您正在传递完整的日期时间字符串,并尝试将其仅视为日期或时间,这是不正确的。 Instead I suggest you to parse DateTime object with both date and time, and then take whatever you need from parsed object: 相反,我建议您使用日期和时间来解析DateTime对象,然后从解析的对象中获取所需的内容:
CultureInfo enUS = CultureInfo.CreateSpecificCulture("en-us");
DateTime dt = DateTime.ParseExact(CompletionDate, "yyyy-MM-dd HH:mm:ss", enUS);
var ProjectEDate = dt.Date.ToString();
var ProjectETime = dt.TimeOfDay.ToString();
DateTime.ParseExact(CompletionDate, "yyy-MM-dd", ...
You are missing 4th 'y' in date format string: 您缺少日期格式字符串中的第4个“ y”:
"yyyy-MM-dd"
^
here
and: String was not recognized as a valid DateTime " format dd/MM/yyyy" 并且: 字符串未被识别为有效的DateTime“格式dd / MM / yyyy”
You need to specify the full format as same as the input string to parse method. 您需要指定与输入字符串相同的完整格式来解析方法。
DateTime dt = DateTime.ParseExact(CompletionDate, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.CreateSpecificCulture("en-us"));
To get results you can use below methods available by default in DateTime. 要获得结果,您可以使用DateTime中默认可用的以下方法。
dt.ToShortTimeString()
"12:58 PM"
dt.ToLongTimeString()
"12:58:00 PM"
dt.ToLongDateString()
"Wednesday, March 29, 2017"
dt.ToShortDateString()
"3/29/2017"
Or you can specify the format to ToString method. 或者,您可以将格式指定为ToString方法。
dt.ToString("yyyy-MM-dd")
"2017-03-29"
dt.ToString("HH:mm:ss")
"12:58:00"
Why do you parse into DateTime
and then convert to a string using ToString
again? 为什么要解析为DateTime
,然后再次使用ToString
转换为字符串? Couldn´t you just simply use String.Split
when all you want is to split the time from the day and you know the exact format ? 当您只想将时间从一天中分出并且知道确切的格式时,是否可以仅使用String.Split
来进行操作?
var CompletionDate = "2017-03-29 12:58:00";
var tmp = CompletionDate.Split(' ');
var ProjectEDate = tmp[0];
var ProjectETime = tmp[1];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.