Click here to Skip to main content
15,917,320 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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:
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.NET
<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));
   }


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

any advice?
Posted
Updated 11-Dec-13 16:49pm
v2

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.
 
Share this answer
 
Comments
melvintcs 11-Dec-13 22:43pm    
tryParse return invalid. my PC is set to "dd MMM, yyyy hh:mm:ss tt" . do you mean i need to set the format same as my PC? this could be a big problem because not every user visit my webpage using the same datetime format.
Abhinav S 12-Dec-13 0:56am    
You need to provide culture in your format.
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
 
Share this answer
 
Try adding the following code into the Global.asax.cs file and test by re-starting your web app:

C#
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;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900