【问题标题】:How to Change the Date Format from MM/dd/yyyy to dd-MM-yyyy?如何将日期格式从 MM/dd/yyyy 更改为 dd-MM-yyyy?
【发布时间】:2015-12-22 07:00:56
【问题描述】:

我想在当前日期添加天数,我创建了代码,但它显示错误字符串未被识别为有效日期时间。我想要这样的日期格式'dd-MM-yyyy'

这是我的脚本

<script>
        function addDate() {
            debugger;
            //Get the entered datevalue
            var enteredDateVal = new moment(document.getElementById("TextBoxStartDate").value);
            //Get the days to add 
            var numberofDays = document.getElementById("TextBoxPredictDays").value
            //Add the days using add method in moment.js
            enteredDateVal.add("days", parseInt(numberofDays));
            //Assign the value in textbox
            document.getElementById("TextBoxPredictedClosing").value = enteredDateVal.format("dd-MM-yyyy");
        }
    </script>

这是我的按钮点击代码

protected void Button9_Click(object sender, EventArgs e)
    {
       // this.TextBoxStartDate ="dd-MM-yyyy";
        DateTime dtval = DateTime.Parse(TextBoxStartDate.Text);
        //Add values here
        DateTime formatteddays = dtval.AddDays(Int16.Parse(TextBoxPredictDays.Text));
        TextBoxPredictedClosing.Text = formatteddays.ToString("dd-MM-yyyy");
    }

我正在为你们所有人更新错误,感谢您的回复,请帮助 这是错误

【问题讨论】:

  • 你在哪里得到这个错误?
  • @AlexZhukovskiy 在 TextBoxStartDate 行
  • 您的代码似乎没问题,但您始终可以使用下面提到的 ParseExact 方法消除任何误解
  • @AlexZhukovskiy 亲爱的检查上面的错误
  • TextBoxStartDate.Text 的值究竟是多少?你能调试并告诉我们吗?

标签: c# asp.net date datetime


【解决方案1】:

使用 DateTime.ParseExact() 从字符串中获取日期对象

protected void Button9_Click(object sender, EventArgs e)
    {
       // this.TextBoxStartDate ="dd-MM-yyyy";
        DateTime dtval = DateTime.ParseExact(TextBoxStartDate.Text, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);
        //Add values here
        DateTime formatteddays = dtval.AddDays(Int16.Parse(TextBoxPredictDays.Text));
        TextBoxPredictedClosing.Text = formatteddays.ToString("dd-MM-yyyy");
    }

您还可以检查字符串是否为有效日期

public static bool IsDate(string tempDate)
        {        
            DateTime fromDateValue;
            var formats = new[] { "dd-MM-yyyy" };
            if (DateTime.TryParseExact(tempDate, formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out fromDateValue))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

你可以参考链接查看日期http://www.niceonecode.com/Q-A/DotNet/CSharp/how-to-check-valid-date-in-c/20271

【讨论】:

  • @Ismail 在解析前使用 IsDate(TextBoxStartDate.Text)
【解决方案2】:

您的代码似乎是正确的,但您的 TextBoxStartDate 格式不正确。

尝试使用

DateTime myDate = DateTime.ParseExact(TextBoxStartDate.Text, "dd-MM-yyyy",
         System.Globalization.CultureInfo.InvariantCulture);
int numVal = Int32.Parse(TextBoxPredictDays.Text);
myDate.AddDays(numVal);

并确保您的输入格式 (dd-MM-yyyy) 适合!

【讨论】:

  • TextBoxStartDate 的输入格式如何?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多