Oct 10 2006

Javascript vs .NET Datetime formatting issues - part 5

Posted by admin under Controls

This is a part of an article serie where we create a ASP.NET 2.0 server control providing date (and time) selection popup . Please start from the beginning.

Being a .NET developer I am used to do datetime formatting the .NET way, using datetime.ToString(...) code.

However the problem is that JavaScript has it's own formatting - and even worse for us - JSCalendar has another set of codes. The latter is because of limitations in the built in Javascript date object - however now we need a way to be able to specify dateformatting the .NET way - but translate it so the Javascript can make some sense of it.



cal1.OutputFormat_NET = "yyyy-MM-dd";
cal1.OutputFormat_NET = "MM/dd/yyyy HH:mm";
cal1.OutputFormat_NET = "MM/dd/yyyy HH:mm tt";



You always specify date format for your "output" textbox using .NET syntax. Internally in our control code we try to translate it  using the GetDaFormat(...) routine:



---inside 
protected override void Render(HtmlTextWriter output)
.....

                sScript += "daFormat        :    \"" + GetDaFormat(OutputFormat_NET) + "\"," + Environment.NewLine;

...
...
..
        private string GetDaFormat(string sFormat)
        {
            //Convert C# format to what's wanted from the JSCalendar
            string sWhat = sFormat.Replace("yyyy", "%Y");
            sWhat = sWhat.Replace("yy", "%y");
            sWhat = sWhat.Replace("MM", "%m");
            sWhat = sWhat.Replace("dd", "%d");
            sWhat = sWhat.Replace("HH", "%H");
            sWhat = sWhat.Replace("mm", "%M");
            sWhat = sWhat.Replace("ss", "%S");
            sWhat = sWhat.Replace("tt", "%p");
            return sWhat;
}