简体   繁体   English

将字符串值(包含日期和时间)分配给两个不同的变量(一个用于日期,另一个用于时间)

[英]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.

相关问题 WPF DateTimePicker或两个文本框(一个用于日期,一个用于时间)相同的数据库绑定,用于显示和处理日期和时间 - WPF DateTimePicker or two textboxes (one for date, one for time) same database binding for show and handle date and time 以自定义格式解析包含日期和时间的字符串 - Parse a string containing date and time in a custom format 在其中一个属性中获取具有最大日期时间值的对象 - Getting object with max date time value in one of it's properties 将日期时间字符串转换为正确的日期时间值时出现问题 - Problem converting date time string to proper date time value 如何将字符串值转换为日期时间值并将其分配给DateTimePicker - How can i convert the string value to date time value and assign it to DateTimePicker Linq to SQL格式日期时间一拍即合 - Linq to SQL Format Date time in One Take Datetime函数将旧日期转换为一个日期 - Datetime function converting one date older time 将包含日期和时间的字符串替换为以 =(等号)为前缀的双引号 - Replacing the String containing Date and Time with Double Quotes prefixed with =(Equal Sign) 将包含日期时间和时区的字符串转换为UTC日期时间 - Convert string containing date time and timezone to UTC datetime 如何使用 DataContractJsonSerializer 序列化包含日期和时间属性的 JSON 字符串? - How to serialize JSON string containing date and time property using DataContractJsonSerializer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM