在添加一些天数并在数据库中存储新旧日期后从文本框中取日期

时间:2018-03-11 08:37:20

标签: c# datetime

我收到日期转换错误,请帮帮我。谢谢大家。

protected void getdate(object sender, EventArgs e)
{
    DateTime dt2 = Convert.ToDateTime(txtdate.Text);
    DateTime dt3 = dt2.AddDays(9);
    txtlastdate.Text = dt3.ToString();        
}

2 个答案:

答案 0 :(得分:0)

您可以使用datetime.ParseExact,您可以手动管理yyyyMMddHHmmss格式;

 DateTime.ParseExact(str, "yyyyMMddHHmmss", CultureInfo.InvariantCulture);

答案 1 :(得分:-1)

尝试添加格式 喜欢这个来自msdn的链接: https://msdn.microsoft.com/en-us/library/zdtaw1bw(v=vs.110).aspx

示例来自格式

的链接
using System;
using System.Globalization;
using System.Threading;

public class Example
{
   public static void Main()
   {
      String[] formats = { "G", "MM/yyyy", @"MM\/dd\/yyyy HH:mm",
                           "yyyyMMdd" };
      String[] cultureNames = { "en-US", "fr-FR" };
      DateTime date = new DateTime(2015, 8, 18, 13, 31, 17);
      foreach (var cultureName in cultureNames) {
         var culture = new CultureInfo(cultureName);
         CultureInfo.CurrentCulture = culture;
         Console.WriteLine(culture.NativeName);
         foreach (var format in formats)
            Console.WriteLine("   {0}: {1}", format,
                              date.ToString(format));
         Console.WriteLine();
      }
   }
}
// The example displays the following output:
//       English (United States)
//          G: 8/18/2015 1:31:17 PM
//          MM/yyyy: 08/2015
//          MM\/dd\/yyyy HH:mm: 08/18/2015 13:31
//          yyyyMMdd: 20150818
//
//       français (France)
//          G: 18/08/2015 13:31:17
//          MM/yyyy: 08/2015
//          MM\/dd\/yyyy HH:mm: 08/18/2015 13:31
//          yyyyMMdd: 20150818