无法转换为日期时间 [英] unable to convert to datetime

查看:65
本文介绍了无法转换为日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从客户端获取日期时间,以下代码能够运行在localhost上运行,但错误消息String未被识别为有效的DateTime。我上传到服务器后。



javascript:

i trying to get the datetime from client, the code below able to run is run at localhost, but error message "String was not recognized as a valid DateTime." after i upload to server.

javascript:

function checkTime(i) {
            if (i < 10) {
                i = "0" + i;
            }
            return i;
        }

        function getClientDT() {
            var today = new Date();
            var year = today.getFullYear();
            var month = today.getMonth() + 1;
            var date = today.getDate();
            var h = today.getHours();
            var m = today.getMinutes();
            var s = today.getSeconds();
            var ampm = h >= 12 ? 'pm' : 'am';

            month = checkTime(month);
            date = checkTime(date);
            //h = checkTime(h);
            h = h % 12;
            h = h ? h : 12;


            m = checkTime(m);
            s = checkTime(s);

            document.getElementById('<%= hfClientDT.ClientID %>').value = date + "/" + month + "/" + year + " " + h + ":" + m + ":" + s + " " +ampm;







<asp:Button ID="btnSubmit" runat="server" CssClass="login" OnClientClick="getClientDT()" OnClick="btnSubmit_Click" Text="Sign In" />
       <asp:HiddenField runat="server" ID="hfClientDT" />





c#



c#

protected void btnSubmit_Click(object sender, EventArgs e)
   {
       string s_DT = this.hfClientDT.Value;
       Response.Write(Convert.ToDateTime(s_DT));
   }





我本地主机的结果是:dd / MM / yyyy hh:mm:ss tt



任何建议?



the result from my localhost is: dd/MM/yyyy hh:mm:ss tt

any advice?

推荐答案

您的计算机设置的默认时间格式是什么?

您始终可以使用 DateTime.TryParse 来确定字符串是否为有效格式。
What default time format is your computer set to?
You can always use DateTime.TryParse to figure out if the string is in a valid format or not.


Hi Melberry,



我认为您的机器日期时间文化信息与您的客户日期时间文化信息不匹配所以

尝试使用cultureinfo类来满足您的全局验证要求您的代码中的日期时间。



您可以将这些网站推荐给初创公司,根据更改它,您将找到您的解决方案



Link1 [ ^ ]

Link2 [ ^ ]

希望这对你有所帮助。



问候,

RK
Hi Melberry,

I think you machine datetime culture info is not matched with your client datetime culture info so
Try to use cultureinfo class for your requirement to globally validate the datetime in your code.

You may refer these sites for startup and according to that change it and you will find your solution

Link1[^]
Link2[^]
Hope this helps you a bit.

Regards,
RK


尝试将以下代码添加到Global.asax.cs文件中并通过重新启动您的Web应用程序进行测试:



Try adding the following code into the Global.asax.cs file and test by re-starting your web app:

using System.Globalization;
using System.Threading;

...

void Application_BeginRequest(object sender, EventArgs e)
{
    CultureInfo culture = (CultureInfo) Thread.CurrentThread.CurrentCulture.Clone();
    culture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
    culture.DateTimeFormat.LongDatePattern = "dd/MM/yyyy";
    culture.DateTimeFormat.ShortTimePattern = "hh:mm:ss tt";
    culture.DateTimeFormat.LongTimePattern = "hh:mm:ss tt";
    Thread.CurrentThread.CurrentCulture = culture;
}


这篇关于无法转换为日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆