簡體   English   中英

使用C#中的函數格式化日期和時間

[英]Formatting Date and Time with a function in C#

所以我從顯卡獲取驅動程序日期並將其顯示到TextBox但值如此20161216000000.000000-000 ,我想將其轉換為實際日期。

我已經有一個函數來轉換這種日期,但這種情況不起作用,使用后它顯示我喜歡這個01-01-0001 00:00:00

這是我的功能代碼:

private static string FormatDateTime(object ObjInstallDate)
{
    object FinalDate = DBNull.Value;
    string strDate = Convert.ToString(ObjInstallDate);
    DateTime dtm;
    DateTime.TryParseExact(strDate, new string[] { "yyyyMMdd", "yyyy-MM-dd", "dd-MM-yyyy" },
        System.Globalization.CultureInfo.InvariantCulture,
        System.Globalization.DateTimeStyles.None, out dtm);
    if (!String.IsNullOrEmpty(strDate))
    {
        FinalDate = dtm;
    }
    return FinalDate.ToString();
}

你知道我怎么能在這個案例20161216000000.000000-000這樣的2016-12-16

獲取子字符串可以完成工作(如果格式總是如圖所示):

DateTime.TryParseExact(strDate.Substring(0, 8), "yyyyMMdd",
    System.Globalization.CultureInfo.InvariantCulture,
    System.Globalization.DateTimeStyles.None, out DateTime dtm);

要獲得所需的格式以顯示您可以使用的結果

dtm.ToString("yyyy-MM-dd");

在查看您的代碼和問題之后,看起來您傳遞給函數的日期不是c#支持的正確/預期格式,這就是它為您提供默認系統的開始日期01-01-0001 00:00的原因這里00

但是,作為一個工作,我可以觀察到輸入值的前8位是日期部分,所以你可以按照以下方式使用它:

DateTime.TryParseExact(strDate.Substring(0, 8), "yyyyMMdd",
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None, out dtm);

return dtm.ToString("yyyy-MM-dd");

您只需要確保您的格式符合您的輸入,而您提供的輸入格式都沒有。

查看自定義dateformat說明符和文字字符與輸入的對齊方式

  Your input: 20161216000000.000000-000 format specifiers: yyyyMMddhhmmss.ffffff-000 

將這種格式帶到您的方法,你會得到這個:

// takes an object, returns a DateTime or null in case of failure
DateTime FormatDateTime(object ObjInstallDate)
{
    DateTime dtm;
    if (!DateTime.TryParseExact(
        (string) ObjInstallDate, // notice that we don't hassle with the input here, 
                                 // only cast to a string
                                 // we simply rely on the parser of DateTimer.TryParseExact
        "yyyyMMddhhmmss.ffffff-000",  // this matches the format
                                      // if the -000 represents a timezone
                                      // you can use z00 or zz0 
                                      // don't use zzz as that expects -0:00
        System.Globalization.CultureInfo.InvariantCulture,
        System.Globalization.DateTimeStyles.None, 
        out dtm)) 
    {
        // an invalid date was supplied, maybe throw, at least log
        Console.WriteLine("{0} is not valid", ObjInstallDate);
    }
    // we either return here null or a valid date
    return dtm; // !! No ToString() here
}

我從自定義日期和時間格式字符串中選擇了所需的自定義格式值。

請注意,我只返回已創建的DateTime實例。 你會看到我為什么這么做的原因。

因為你想在Winform上顯示DateTime(我假設在文本框中,但標簽也可以工作),你現在可以簡單地將DateTime實例數據綁定到文本框,讓數據綁定管道進行格式化。 這是一個可以在LinqPad中運行的代碼示例:

// take a string and make a date of it
var str = "20161216000000.000000-000";
DateTime dtm = FormatDateTime(str);

// show it on a Databind TextBox
var f = new Form();
var txt = new TextBox();
txt.Width = 200;
// bind the Text property
txt.DataBindings.Add(
    "Text",  // property on the textbox
    dtm, // our DateTime object
    null, // use the DateTime instance, not a property
    true, // use formatting 
    DataSourceUpdateMode.OnValidation, 
    null, // value to indicate null
    "yyyy-MM-dd"); // our format
f.Controls.Add(txt);
f.Show();

我在DataBindingsCollection上使用Add的重載,它接受一個Format字符串。 然后,我可以使用相同的自定義格式說明符選項來表示我想要的DateTime實例。 從這里可以很容易地添加另一個TextBox,它使用相同的DateTime實例,但在文本中顯示月份。

當所有這些結合在一起時,這將是您的結果:

winform顯示2016-12-01作為日期的文本框

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM