Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / March 2006



Tip: Looking for answers? Try searching our database.

Date Format

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bob Sanderson - 28 Mar 2006 17:06 GMT
I would like to use a date picker on a web page input form. I found one
which does what I want but the date format it outputs is not correct for my
form. The script contains the following:

// datetime parsing and formatting routimes. modify them if you wish other
datetime format
function str2dt (str_datetime) {
    var re_date = /^(\d+)\-(\d+)\-(\d+)\s+(\d+)\:(\d+)\:(\d+)$/;
    if (!re_date.exec(str_datetime))
        return alert("Invalid Datetime format: "+ str_datetime);
    return (new Date (RegExp.$3, RegExp.$2-1, RegExp.$1, RegExp.$4,
RegExp.$5, RegExp.$6));
}
function dt2dtstr (dt_datetime) {
    return (new String (
            dt_datetime.getDate()+"-"+(dt_datetime.getMonth()+1)
+"-"+dt_datetime.getFullYear()+" "));
}
function dt2tmstr (dt_datetime) {
    return (new String (
            dt_datetime.getHours()+":"+dt_datetime.getMinutes()
+":"+dt_datetime.getSeconds()));
}

I am not familiar with JavaScript and have not been able to figure out what
changes I need to make in order to get the output I want. I tried to
contact the author, but his email address no longer works. I would greatly
appreciate it if someone could tell me what modifications are necessary.

The output format I'm looking for is YYYY-MM-DD (no time).
web.dev - 28 Mar 2006 18:54 GMT
> I would like to use a date picker on a web page input form. I found one
> which does what I want but the date format it outputs is not correct for my
> form. The script contains the following:

[snip]

> function dt2dtstr (dt_datetime) {
>     return (new String (
>             dt_datetime.getDate()+"-"+(dt_datetime.getMonth()+1)
> +"-"+dt_datetime.getFullYear()+" "));
> }

[snip]

> The output format I'm looking for is YYYY-MM-DD (no time).

It appears at first glance this is the function you want to modify.
I'm assuming the local variable dt_datetime is a Date object,
therefore, all you need to do is change it to the following:

function dt2dtstr(dt_datetime)
{
  return dt_datetime.getFullYear() + "-" + (dt_datetime.getMonth + 1)
+ "-" + dt_datetime.getDate();
}
Dr John Stockton - 28 Mar 2006 22:17 GMT
JRS:  In article <Xns9794710F3FD12centroidincearthlink@207.69.189.191>,
dated Tue, 28 Mar 2006 16:06:50 remote, seen in
news:comp.lang.javascript, Bob Sanderson <sandman@LUVSPAMsandmansoftware.
com> posted :
>I would like to use a date picker on a web page input form. I found one
>which does what I want but the date format it outputs is not correct for my
[quoted text clipped - 9 lines]
>RegExp.$5, RegExp.$6));
>}

Don't allow your posting agent to wrap your code.  Posted code should be
directly executable.  Tabs are unpopular in News.

There,   return alert(...)   is unusual but should be OK.

The above will allow a date such as 29-02-29.

RegExp.$n seems deprecated nowadays; String.match() is often preferred.

>function dt2dtstr (dt_datetime) {
>       return (new String (
[quoted text clipped - 9 lines]
>I am not familiar with JavaScript and have not been able to figure out what
>changes I need to make in order to get the output I want.

You could try putting the obvious references to year, month, day in that
order.  In the above, new String() is not needed.

> I tried to
>contact the author, but his email address no longer works. I would greatly
>appreciate it if someone could tell me what modifications are necessary.
>
>The output format I'm looking for is YYYY-MM-DD (no time).

You should be requiring leading zeroes there, of course; ignore any
solution offered that shows no sign of providing them.  I use

       function LZ(x) { return (x>=10||x<0?"":"0") + x }

but in this case it's safe to omit    ||x<0  .

function dt2dtstr(D) {
 return D.getFullYear()+"-"+LZ(D.getMonth()+1)+"-"+LZ(D.getDate())+" " }

Read the newsgroup FAQ; see below.

Signature

© John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v4.00   IE 4 ©
<URL:http://www.jibbering.com/faq/>  JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

Thomas 'PointedEars' Lahn - 29 Mar 2006 14:08 GMT
> [...] Bob Sanderson [...] posted:
>> [...]
[quoted text clipped - 8 lines]
> [...]
> RegExp.$n seems deprecated nowadays; String.match() is often preferred.
In this case, String.prototype.match() does not need to be
used even if one wants to avoid the deprecated references:

 var m;  // m for match
 if ((m = re_date.exec(str_datetime)))
 {
   return new Date(m[3], m[2] - 1, m[1], m[4], m[5], m[6]);
 }
 else
 {
   return alert("Invalid Datetime format: "+ str_datetime);
 }


PointedEars
John G Harris - 30 Mar 2006 20:38 GMT
>JRS:  In article <Xns9794710F3FD12centroidincearthlink@207.69.189.191>,
>dated Tue, 28 Mar 2006 16:06:50 remote, seen in
>news:comp.lang.javascript, Bob Sanderson <sandman@LUVSPAMsandmansoftware.
>com> posted :

 <snip>
>>The output format I'm looking for is YYYY-MM-DD (no time).
>
>You should be requiring leading zeroes there, of course;
 <snip>

This is also user-hostile, of course.

 John
Signature

John Harris

Dr John Stockton - 31 Mar 2006 14:06 GMT
JRS:  In article <TkhsYPLTPDLEFwjf@jgharris.demon.co.uk>, dated Thu, 30
Mar 2006 20:38:59 remote, seen in news:comp.lang.javascript, John G
Harris <john@nospam.demon.co.uk> posted :
>>JRS:  In article <Xns9794710F3FD12centroidincearthlink@207.69.189.191>,
>>dated Tue, 28 Mar 2006 16:06:50 remote, seen in
[quoted text clipped - 8 lines]
>
>This is also user-hostile, of course.

I don't see your point.  Is the user the OP, or those who read his
pages?

He asks for output YYYY-MM-DD (which is as per ISO 8601); but it's not
clear whether he positively wants fixed-width fields.  His existing code
does not give leading zeroes, nor does that of web_dev.

Requiring leading zeroes on input would be another matter.  Requiring it
of /hoi polloi/ may confuse the innumerate - the concept of zero as a
digit was fairly new to the west when Columbus sailed - but requiring it
of intelligent or practised users is a precaution against errors such as
an inadequately-depressed key.

The set of acceptable input formats needs to include the set of output
formats, but may be larger.  That requites careful thought if the
applicable localisation may vary.

Signature

© John Stockton, Surrey, UK.  ???@merlyn.demon.co.uk   Turnpike v4.00   MIME. ©
Web  <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
    Check boilerplate spelling -- error is a public sign of incompetence.
   Never fully trust an article from a poster who gives no full real name.

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.