JavaScript version of VBScript's formatting functions

M

Mark Rae

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
 
G

Guest

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.
 
B

Bruce Barker

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)
 
M

Mark Rae

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

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
 
M

matt

Mark said:
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.

As the author, I can tell you that this code is used on thousands of
web sites, and it is quite solid ;)
However, if you pass the strings '15 Sep 2005' or '15 September 2005'
through the isDate() function, it returns false - that's rubbish!

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/
 
E

Eliyahu Goldin

Mark,

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

Eliyahu
 
E

Eliyahu Goldin

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
 
M

matt

Mark said:
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!"

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.
From the code comments:

// ------------------------------------------------------------------
// 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
 
M

Mark Rae

Matt,

I clearly owe you an apology... :)
but perhaps 'dd MMM yyyy' should be included in there also :)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top