将YYYYMMDDHHMMSS的字符串值格式转换为C#DateTime

将YYYYMMDDHHMMSS的字符串值格式转换为C#DateTime,c#,datetime,C#,Datetime,我需要将格式为“YYYYMMDDHHMMSS”的字符串值转换为日期时间。但不确定如何,可能是一个日期时间。Tryparse可以用来实现这一点。或者有没有其他方法可以做到这一点。我可以使用一些字符串操作单独获取“YYYYMMDD”,转换为日期时间,然后分别向该日期时间添加HH、MM、SS。但是,是否有任何DateTime.TryParse()方法可以在一行中使用,将“YYYYMMDDHHMMSS”格式的字符串值转换为DateTime值?您必须使用。我还建议包含不变的区域性,以确定此格式与任何区域性

我需要将格式为“YYYYMMDDHHMMSS”的字符串值转换为日期时间。但不确定如何,可能是一个日期时间。Tryparse可以用来实现这一点。或者有没有其他方法可以做到这一点。我可以使用一些字符串操作单独获取“YYYYMMDD”,转换为日期时间,然后分别向该日期时间添加HH、MM、SS。但是,是否有任何DateTime.TryParse()方法可以在一行中使用,将“YYYYMMDDHHMMSS”格式的字符串值转换为DateTime值?

您必须使用。我还建议包含不变的区域性,以确定此格式与任何区域性无关。此外,它还可以防止在某些代码分析工具中出现警告

var date = DateTime.ParseExact(value, "yyyyMMddHHmmss", CultureInfo.InvariantCulture);

定义您自己要使用的解析格式字符串

string formatString = "yyyyMMddHHmmss";
string sample = "20100611221912";
DateTime dt = DateTime.ParseExact(sample,formatString,null);
如果日期时间为毫秒,请使用以下formatString

string format = "yyyyMMddHHmmssfff"
string dateTime = "20140123205803252";
DateTime.ParseExact(dateTime ,format,CultureInfo.InvariantCulture);
谢谢

课程
{
静态void Main(字符串[]参数)
{
int transactionDate=20201010;
int?交易时间=210000;
var agreementDate=DateTime.Today;
var previousDate=协议日期。添加天数(-1);
var agreementHour=22;
var agreementMinute=0;
var agreementSecond=0;
var startDate=新日期时间(previousDate.Year,previousDate.Month,previousDate.Day,agreementHour,agreementMinute,agreementSecond);
var endDate=新的日期时间(agreementDate.Year、agreementDate.Month、agreementDate.Day、agreementHour、agreementMinute、agreementSecond);
DateTime selectedDate=Convert.ToDateTime(transactionDate.ToString().Substring(6,2)+“/”+transactionDate.ToString().Substring(4,2)+“/”+transactionDate.ToString().Substring(0,4)+“+string.Format(“{0:00:00}”,transactionTime));
Console.WriteLine(“所选日期:+selectedDate.ToString());
Console.WriteLine(“开始日期:+startDate.ToString());
Console.WriteLine(“结束日期:+endDate.ToString());

如果(selectedDate>startDate&&selectedDate的精确副本实际上,这是原始问题的另一面。这样做几乎值得a-1。我同意。但我不知道这太直截了当了。你在问第二个问题前半小时问了第一个问题。你可以将原始问题修改为I包括这一点,而不是问一个全新的问题。坦率地说,第一个问题的答案为您提供了回答此问题所需的几乎所有信息。选定日期:10.10.2020 21:00:00开始日期:8.10.2020 22:00:00结束日期:9.10.2020 22:00:00大于结束日期!您至少可以提供一些代码的基本描述吗根据你的评论,我不确定这是答案还是问题,你发布了什么
class Program
{
    static void Main(string[] args)
    {

        int transactionDate = 20201010;
        int? transactionTime = 210000;

        var agreementDate = DateTime.Today;
        var previousDate = agreementDate.AddDays(-1);

        var agreementHour = 22;
        var agreementMinute = 0;
        var agreementSecond = 0;

        var startDate = new DateTime(previousDate.Year, previousDate.Month, previousDate.Day, agreementHour, agreementMinute, agreementSecond);
        var endDate = new DateTime(agreementDate.Year, agreementDate.Month, agreementDate.Day, agreementHour, agreementMinute, agreementSecond);

        DateTime selectedDate = Convert.ToDateTime(transactionDate.ToString().Substring(6, 2) + "/" + transactionDate.ToString().Substring(4, 2) + "/" + transactionDate.ToString().Substring(0, 4) + " " + string.Format("{0:00:00:00}", transactionTime));

        Console.WriteLine("Selected Date : " + selectedDate.ToString());
        Console.WriteLine("Start Date : " + startDate.ToString());
        Console.WriteLine("End Date : " + endDate.ToString());

        if (selectedDate > startDate && selectedDate <= endDate)
            Console.WriteLine("Between two dates..");
        else if (selectedDate <= startDate)
            Console.WriteLine("Less than or equal to the start date!");
        else if (selectedDate > endDate)
            Console.WriteLine("Greater than end date!");
        else
            Console.WriteLine("Out of date ranges!");
    }
}