PDA

View Full Version : .NET DateTime Format to ExtJs DateTime format



Chods
7 May 2009, 5:43 AM
Hi

Does anyone know if there is a good way to convert a .net dateTime format to an ExtJs datetime format.

There is a reference here but I am trying to avoid writing my own translation method.

Also, if this is not possible then is there a complete set of localization files for ExtJs which contains all the dateTime formats for each language_region (i.e en_GB ) for example. The source/locale folders only hold about 40 or which only a few are defined with region aswell as language. Even en-US does not exist and this is a big problem for us and might sway out decision on using the ExtJs framework outright.

Thanks in advance!

geoffrey.mcgill
7 May 2009, 9:04 AM
Hi Chods,

The following two methods should help with converting the format strings server-side.


/// <summary>
/// Accepts a Unix/PHP date string format and returns a valid .NET date format
/// </summary>
/// <param name="format">The PHP format string</param>
/// <returns>The format string converted to .NET DateTime format specifiers</returns>
public static string ConvertPHPToNet(string format)
{
if (string.IsNullOrEmpty(format))
{
return "";
}

StringBuilder final = new StringBuilder(128);
string temp = "";
Match m = Regex.Match(format, @"(%|\\)?.|%%", RegexOptions.IgnoreCase);

while(m.Success)
{
temp = m.Value;

if(temp.StartsWith(@"\") || temp.StartsWith("%%"))
{
final.Append(temp.Replace(@"\", "").Replace("%%", "%"));
}

switch(temp)
{
case "d":
final.Append("dd");
break;
case "D":
final.Append("ddd");
break;
case "j":
final.Append("d");
break;
case "l":
final.Append("dddd");
break;
case "F":
final.Append("MMMM");
break;
case "m":
final.Append("MM");
break;
case "M":
final.Append("MMM");
break;
case "n":
final.Append("M");
break;
case "Y":
final.Append("yyyy");
break;
case "y":
final.Append("yy");
break;
case "a":
case "A":
final.Append("tt");
break;
case "g":
final.Append("h");
break;
case "G":
final.Append("H");
break;
case "h":
final.Append("hh");
break;
case "H":
final.Append("HH");
break;
case "i":
final.Append("mm");
break;
case "s":
final.Append("ss");
break;
default:
final.Append(temp);
break;
}
m = m.NextMatch();
}

return final.ToString();
}

/// <summary>
/// Accepts a Unix/PHP date string format and returns a valid .NET date format
/// </summary>
/// <param name="format">The .NET format string</param>
/// <returns>The format string converted to PHP format specifiers</returns>
public static string ConvertNetToPHP(string format)
{
if (string.IsNullOrEmpty(format))
{
return "";
}

StringBuilder final = new StringBuilder(128);
string temp = "";

switch (format.Trim())
{
case "d":
format = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
break;
case "D":
format = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.LongDatePattern;
break;
case "t":
format = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern;
break;
case "T":
format = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern;
break;
}

Match m = Regex.Match(format, @"(\\)?(dd?d?d?|MM?M?M?|yy?y?y?|hh?|HH?|mm?|ss?|tt?|S)|.", RegexOptions.IgnoreCase);

while (m.Success)
{
temp = m.Value;

switch (temp)
{
case "dd":
final.Append("d");
break;
case "ddd":
final.Append("D");
break;
case "d":
final.Append("j");
break;
case "dddd":
final.Append("l");
break;
case "MMMM":
final.Append("F");
break;
case "MM":
final.Append("m");
break;
case "MMM":
final.Append("M");
break;
case "M":
final.Append("n");
break;
case "yyyy":
final.Append("Y");
break;
case "yy":
final.Append("y");
break;
case "tt":
final.Append("a");
break;
case "h":
final.Append("g");
break;
case "H":
final.Append("G");
break;
case "hh":
final.Append("h");
break;
case "HH":
final.Append("H");
break;
case "mm":
final.Append("i");
break;
case "ss":
final.Append("s");
break;
default:
final.Append(temp);
break;
}
m = m.NextMatch();
}

return final.ToString();
}

geoffrey.mcgill
7 May 2009, 9:20 AM
There's also some helper functions in the Datejs (http://www.datejs.com/) library that might come in handy for making the conversions client-side, see http://code.google.com/p/datejs/source/browse/trunk/src/extras.js

Hope this helps.

Chods
8 May 2009, 12:33 AM
So am i right in saying that ExtJs uses the PHP/Unix conventions for date formatting strings?

The client side datejs functions will come in handy. But I think what i am really after is a way of converting a .net date time format string into the ExtJs standard. So the opposite of normalize format in the extras.js file. Is this available anywhere?

Regards

geoffrey.mcgill
8 May 2009, 7:41 AM
So am i right in saying that ExtJs uses the PHP/Unix conventions for date formatting strings?

Correct. As per the docs for the Date class (http://extjs.com/deploy/dev/docs/?class=Date):


The date parsing and format syntax is a subset of PHP's date() function, and the formats that are supported will provide results equivalent to their PHP versions.

..


The client side datejs functions will come in handy. But I think what i am really after is a way of converting a .net date time format string into the ExtJs standard. So the opposite of normalize format in the extras.js file. Is this available anywhere?

I don't think a function to convert a .NET format into PHP is available in the Datejs library, but it should be easy enough to write. It would be the opposite of the .format() function. And the logic is also basically the same as the C# ConvertNetToPHP Method.

Hope this helps.

mystix
9 May 2009, 9:44 AM
The client side datejs functions will come in handy. But I think what i am really after is a way of converting a .net date time format string into the ExtJs standard.

in the official 3.0rc1/rc1.1 download there's a new M$ format you might want to take a look at:
http://extjs.com/deploy/ext-3.0-rc1.1/docs/?class=Date

it parses a .net date string of the form "\/Date(12345678901234)\/" into a js Date object.


p.s. there's no "ExtJS standard" for dates.
we're really only working with standard javascript Date objects.