Groups | Blog | Home
all groups > asp.net > september 2005 >

asp.net : JavaScript version of VBScript's formatting functions


Curt_C [MVP]
9/14/2005 1:29:12 PM
No site is going to be 100% guaranteed... heck, nothings perfect.
As for "best you will find"... well, that's another story.
I've always used www.DevGuru.com for my javascript syntax and found it quite
nice.

--
Curt Christianson
site: http://www.darkfalz.com
blog: http://blog.darkfalz.com



[quoted text, click to view]
Bruce Barker
9/14/2005 6:28:13 PM
its a pretty simple list. toString() that returns the local version, there
is an options radix, used to specify the radix used for numberss

full spec :
http://www.ecma-international.org/publications/standards/Ecma-262.htm

javascript has the following objects types:

object toString() results
------- ----------------------------
Array does to a toString of all object sand concats the results
Boolean return "true" or "false"
Date returns date in locale format
Function return func def as string
Error return error message
Number return number as string based on radix (default = 10)
String returns string value
Default return "[object objectname]"

the Date object has some additional string functions (none of these take a
parameter, and return the obvious):

toDateString()
toGMTString()
toLocaleDateString()
toLocaleString()
toLocaleTimeString()
toTimeString()
toUTCString()

the Number object has the additional following:

toFixed (factionDigits)
toLocalString()
toPrecision(precision)


most sites write their own formating utilities

-- bruce (sqlwork.com)









[quoted text, click to view]

Mark Rae
9/14/2005 8:21:57 PM
Hi,

Does anyone know of a "definitive" source of JavaScript formatting functions
e.g. to format numbers, dates, currencies etc?

There are loads of examples on the Internet, of course, but I've yet to find
any which are 100% reliable.

Mark

Eliyahu Goldin
9/15/2005 12:00:00 AM
Mark,

I have personal experience with http://www.mattkruse.com/javascript/date/
for date formatting. It is good.

Eliyahu

[quoted text, click to view]

Mark Rae
9/15/2005 12:00:00 AM
[quoted text, click to view]

Eliyahu,

[quoted text, click to view]

This is precisely what I'm talking about! On the surface this looks
excellent, just the way that hundreds of other such examples on the Internet
look excellent.

However, if you pass the strings '15 Sep 2005' or '15 September 2005'
through the isDate() function, it returns false - that's rubbish!

Mark

Eliyahu Goldin
9/15/2005 12:00:00 AM
Mark,

Could be isDate() is not good. I am using it only for formatting the dates
and never had any issues so far.

Eliyahu

[quoted text, click to view]

Eliyahu Goldin
9/15/2005 12:00:00 AM
Wow, here is the author!

Matt, I just take an opportunity to thank you for the code. For me it is
really good, appreciated.

Eliyahu

[quoted text, click to view]

Mark Rae
9/15/2005 12:00:00 AM
[quoted text, click to view]

Matt,

[quoted text, click to view]

I may be maligning you unfairly. I simply went to the site that Eliyahu
mentioned (http://www.mattkruse.com/javascript/date/), entered the string
'15 Sep 2005' in the Free-Form Date Field text box, and clicked
parseDate() - the message was "Date string does not match any recognized
formats!"

Mark

matt NO[at]SPAM mattkruse.com
9/15/2005 6:37:09 AM
[quoted text, click to view]

As the author, I can tell you that this code is used on thousands of
web sites, and it is quite solid ;)

[quoted text, click to view]

The isDate() function takes a date format to validate against, as could
be expected of any isDate() function.

If you enter '15 Sep 2005' or '15 September 2005' with the date format
'dd MMM yyyy' it says true. Just as expected.

Where's the problem?

Matt Kruse
http://www.mattkruse.com/
matt NO[at]SPAM mattkruse.com
9/15/2005 8:33:00 AM
[quoted text, click to view]

This is true. There are two different things, here:

isDate() requires a date format to validate against. If you specify the
correct date format, isDate will correctly tell you that '15 Sep 2005'
is valid.

parseDate() tries to parse a free-form string into a date object. Since
there are obviously many different possible date formats you can enter,
it checks some common formats. It's meant as a last-resort for figuring
out a date that a user entered into a free-form field. It's always
preferred that a format is required and specified for the user, and
their value is validated against the format.

[quoted text, click to view]

// ------------------------------------------------------------------
// parseDate( date_string [, prefer_euro_format] )
//
// This function takes a date string and tries to match it to a
// number of possible date formats to get the value. It will try to
// match against the following international formats, in this order:
// y-M-d MMM d, y MMM d,y y-MMM-d d-MMM-y MMM d
// M/d/y M-d-y M.d.y MMM-d M/d M-d
// d/M/y d-M-y d.M.y d-MMM d/M d-M
// A second argument may be passed to instruct the method to search
// for formats like d/M/y (european format) before M/d/y (American).
// Returns a Date object or null if no patterns match.
// ------------------------------------------------------------------

The format you're looking for, 'dd MMM yyyy' isn't one of the formats
that are automatically checked, so it will not recognize your date.

You can either use the getDateFromFormat function, which takes a string
and a format, or you can modify the parseDate code to check for more
date formats that you wish to check for. I got most of the common ones,
but perhaps 'dd MMM yyyy' should be included in there also :)

Matt Kruse
http://www.mattkruse.com
Mark Rae
9/15/2005 8:50:48 AM
[quoted text, click to view]

Yes indeed - I was wondering if anyone had found one which was 100%
efficient, because I haven't...

Mark Rae
9/15/2005 6:01:45 PM
[quoted text, click to view]

Matt,

I clearly owe you an apology... :-)

[quoted text, click to view]

Well, that's the format I always try first, for three principal reasons:

1) it's guaranteed to be totally unambiguous as opposed to, say, 03/04/05

2) it's guaranteed to be Y2K compatible

3) it's guaranteed to give the same results against SQL Server, no matter
which culture settings were used when the server was built.

It's also pretty much the de facto default format in the UK...

Mark

AddThis Social Bookmark Button