473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Date Format

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).
Mar 28 '06 #1
5 4560

Bob Sanderson wrote:
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();
}

Mar 28 '06 #2
JRS: In article <Xn**********************************@207.69.189.1 91>,
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
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));
}
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 (
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.
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.

--
© 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.
Mar 28 '06 #3
Dr John Stockton wrote:
[...] Bob Sanderson [...] posted:
[...]
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));
}


[...]
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
Mar 29 '06 #4
In article <MS**************@merlyn.demon.co.uk>, Dr John Stockton
<jr*@merlyn.demon.co.uk> writes
JRS: In article <Xn**********************************@207.69.189.1 91>,
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
--
John Harris
Mar 30 '06 #5
JRS: In article <Tk**************@jgharris.demon.co.uk>, dated Thu, 30
Mar 2006 20:38:59 remote, seen in news:comp.lang.javascript, John G
Harris <jo**@nospam.demon.co.uk> posted :
In article <MS**************@merlyn.demon.co.uk>, Dr John Stockton
<jr*@merlyn.demon.co.uk> writes
JRS: In article <Xn**********************************@207.69.189.1 91>,
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.


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.

--
© 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.
Mar 31 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Simon Brooke | last post by:
I'm investigating a bug a customer has reported in our database abstraction layer, and it's making me very unhappy. Brief summary: I have a database abstraction layer which is intended to...
2
by: amith | last post by:
hi I have written javascript for comparing two dates in US format and finding out whether the start date is greater than the end date and vice versa. In this attempt i have instantiated the...
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
3
by: Lyn | last post by:
Hi, I am developing a project in which I am checking for records with overlapping start/end dates. Record dates must not overlap date of birth, date of death, be in the future, and must not...
5
by: Macca | last post by:
Hi, I have a table which has a date/time field. I am storing them as follows :- 01/01/2005 11:25 01/01/2005 19:44 02/01/2005 05:04
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
20
by: andreas | last post by:
When I copy a vb.net project using date formats from one PC with a windows date format f.e. dd/mm/yyyy to another PC having a format yy/mm/dd then I get errors. How can I change for a while in the...
30
by: fniles | last post by:
On my machine in the office I change the computer setting to English (UK) so the date format is dd/mm/yyyy instead of mm/dd/yyyy for US. This problem happens in either Access or SQL Server. In the...
4
by: OzNet | last post by:
I have some functions to calculate the working days in a given period. This includes a table that is queried to calculate the number of public holidays that don’t occur on a weekend. If I test...
11
ollyb303
by: ollyb303 | last post by:
Hello, I am using a dynamic crosstab report to track performance statistics for my company and I have hit a problem. I would like the option to track stats daily (for the last 7 complete...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.