473,796 Members | 2,560 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Formatting strings

I'm a complete novice to JS. I want to insert the date and time into a
document in the format:

WB-MMDDHHmm

Where:

WB- is a fixed string prefix (the whole string is a reference number)
MM is the month 01 to 12, with a leading 0 if required
DD is the day 01 to 31, with a leading 0 if required
HH is the hour 00 to 23, with a leading 0 if required
mm is the minute 00 to 59, with a leading 0 if required
I've got this far:
<script language="JavaS cript">

function ShowDateTime()
{
var today = new Date();
var mo=today.getMon th()+1;
var da=today.getDat e();
var ho=today.getHou rs();
var mi=today.getMin utes();

# parse here #

document.write( "WB-"+mo+da+ho+ mi);
}

</script>
The problem is the bit to parse the strings, I've tried

if (mo.length < 2) mo="0"+mo; ... etc

but it doesn't work.

Help!
--
Nige

Please replace YYYY with the current year
ille quis mortem cum maximus ludos, vincat
Jul 20 '05 #1
26 2795
Nige wrote:
function ShowDateTime()
{
var today = new Date();
var mo=today.getMon th()+1;
var da=today.getDat e();
var ho=today.getHou rs();
var mi=today.getMin utes();

# parse here #

document.write( "WB-"+mo+da+ho+ mi);
}
[...]

The problem is the bit to parse the strings, I've tried

if (mo.length < 2) mo="0"+mo; ... etc

but it doesn't work.


The Date.get...(... ) methods return values of type `number'. When you assign
them to a variable and use the lookup operator `.' with that variable, the
value stored in that variable is converted to a Number object.
Unfortunately, those objects do not have a `length' property, so the value
is `undefined'. But `undefined < 2' equals `false' (as you can and should
test with alert(...) or document.write( ...)) so the second assignment to
`mo' aso. is not executed.

I see two possible approaches to solve the problem:

A) Convert the number into a string. String objects have a
length property you can compare with:

if (String(mo).len gth < 2)
...

B) What you need is only the leading zero for a two-digit number.
So concatenate it only when it is needed, as the number is less
than 10:

if (mo < 10)
...

I use and recommend to use B) since it is faster and allocates less memory.
HTH

PointedEars

Jul 20 '05 #2
In comp.lang.javas cript, Thomas 'PointedEars' Lahn wrote:
The Date.get...(... ) methods return values of type `number'.


Of course, thank you.

I've since realized that the problem is more complex than I thought. The
plan was to generate a reference number consisting of the date and time
when a user submits a form.

The reference number is created by the CGI script that processes the
form, but (of course) if the user's clock is not the same as the server
then the numbers are different. D'OH!

Is there a way to generate a string referencing the current date and
time (as MMDDHHmm) when a user presses the Submit button, send this
string with the other form data, then display this string on the page
that is displayed next?

--
Nige

Please replace YYYY with the current year
ille quis mortem cum maximus ludos, vincat
Jul 20 '05 #3
Nige wrote:
I've since realized that the problem is more complex than I thought. The
plan was to generate a reference number consisting of the date and time
when a user submits a form.

The reference number is created by the CGI script that processes the
form, but (of course) if the user's clock is not the same as the server
then the numbers are different. D'OH!


Null problemo. You should never use client-side data when server-side data
is more reliable. Did you think of timezones and stuff like that? Use the
server time for generating the reference number which also frees you from
dependence of client-side JavaScript support and unreliable techniques (like
changing the value of an `input' element on submit which could, but should
not be done here.)
PointedEars

P.S.
Please don't change the Subject unless there is really a change of subject.

Jul 20 '05 #4
In comp.lang.javas cript, Thomas 'PointedEars' Lahn wrote:
Use the server time for generating the reference number


I don't know how to make the CGI script send a value to the next page.

--
Nige

Please replace YYYY with the current year
ille quis mortem cum maximus ludos, vincat
Jul 20 '05 #5
In comp.lang.javas cript, Thomas 'PointedEars' Lahn wrote:
You should never use client-side data when server-side data
is more reliable.


Time zones don't enter into it, all users are in the UK. All I really
need is a reference number that gives the date and approx time, it
doesn't even have to be unique, but the user must be shown the same
number that I get sent by the CGI.

As I said in my other post (which I sent without giving full details,
sorry), I can't send the CGI data to the next page. So the simple option
(assuming it is simple) is the generate a string on the form page, send
it with the CGI data, and display it on the following page.

Can it be done?
--
Nige

Please replace YYYY with the current year
ille quis mortem cum maximus ludos, vincat
Jul 20 '05 #6
Nige wrote:
In comp.lang.javas cript, Thomas 'PointedEars' Lahn wrote:
Use the server time for generating the reference number


I don't know how to make the CGI script send a value to the next page.


This is off-topic here (since it has nothing to do with JavaScript)
but what kind of CGI script are you talking about?
PointedEars

P.S.
Don't expect people to manipulate the `To' when they send you e-mail.

Jul 20 '05 #7
In article <bo************ *@ID-107532.news.uni-berlin.de>, Thomas 'PointedEars'
Lahn <Po*********@we b.de> writes:
Nige wrote:
In comp.lang.javas cript, Thomas 'PointedEars' Lahn wrote:
Use the server time for generating the reference number
I don't know how to make the CGI script send a value to the next page.


This is off-topic here (since it has nothing to do with JavaScript)
but what kind of CGI script are you talking about?


Server Side Javascript potentially running as a CGI is off-topic in a
Javascript newsgroup?
PointedEars

P.S.
Don't expect people to manipulate the `To' when they send you e-mail.


Dont try to email people from a group and its a non-issue. Post to news, get
answered in news, and theres no problem.
--
Randy
Jul 20 '05 #8
HikksNotAtHome wrote:
In article <bo************ *@ID-107532.news.uni-berlin.de>, Thomas 'PointedEars'
Lahn <Po*********@we b.de> writes:


....
Nige wrote:
In comp.lang.javas cript, Thomas 'PointedEars' Lahn wrote:
Use the server time for generating the reference number

I don't know how to make the CGI script send a value to the next page.


This is off-topic here (since it has nothing to do with JavaScript)
but what kind of CGI script are you talking about?


Server Side Javascript potentially running as a CGI is off-topic in a
Javascript newsgroup?


Of course not, but nothing was said about server-side JavaScript and I
assumed that the OP did not wrote about server-side JavaScript because
he distiguished CGI from that (which is incorrect, though.)
P.S.
Don't expect people to manipulate the `To' when they send you e-mail.


Dont try to email people from a group and its a non-issue. Post to news, get
answered in news, and theres no problem.


Usenet is public newsgroups *and* private (e-)mail (PM), at least because
it is obvious that some things do not belong into newsgroups but are better
to be written via e-mail than not written at all (as the Netiquette
recommends.) I'm sorry for you that you are not aware of this, but that
is your problem, not mine.
EOD

PointedEars

Jul 20 '05 #9
In article <bo************ *@ID-107532.news.uni-berlin.de>, Thomas 'PointedEars'
Lahn <Po*********@we b.de> writes:
Dont try to email people from a group and its a non-issue. Post to news,

get
answered in news, and theres no problem.


Usenet is public newsgroups *and* private (e-)mail (PM), at least because
it is obvious that some things do not belong into newsgroups but are better
to be written via e-mail than not written at all (as the Netiquette
recommends.) I'm sorry for you that you are not aware of this, but that
is your problem, not mine.


I have no problem with it. I dont recieve the mails (If I do, they get
deleted), so I fail to see how Usenet is a "private" place. As I said, don't
try to email someone, no problems. Trying to get outside of the public Usenet
into private Email is crossing a boundary that I don't care to cross. It opens
up too many problems. If the "Netiquette " says its ok, I don't care, I don't do
it and I won't do it.
--
Randy
Jul 20 '05 #10

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

Similar topics

11
1651
by: Steve Holden | last post by:
I was messing about with formatting and realized that the right kind of object could quite easily tell me exactly what accesses are made to the mapping in a string % mapping operation. This is a fairly well-known technique, modified to tell me what keys would need to be present in any mapping used with the format. class Everything: def __init__(self, format="%s", discover=False): self.names = {} self.values =
3
5937
by: Jouke Langhout | last post by:
Hello all! For quite some time now, I've got the following problem: Access won't close properly when a user closes the application. An ACCESS process stays active and that process can only be terminated by pressing ++ and then terminate the process. I searched the entire internet and found out that there could be two things wrong (both of them are mentioned in the bug list on the access
7
3943
by: BBFrost | last post by:
I'm receiving decimal values from database queries and placing them on a report page. The users want to see the following .... Db Value Display Value 123.3400 123.34 123.0000 123 i.e. I want to trim trailing zeros and (decimal point if no decimal values
4
4027
by: Robert Manookian | last post by:
How do you format strings? i.e. In VB6: Format("AB34567", "@@@@@-@@") = "AB345-67" In .Net: ????????
2
2272
by: David Veeneman | last post by:
How does one format a date column in a GridView control? I had assumed that the DataFormat string would do it, but MSDN only shows numeric formatting codes. Can dates be formatted using that property, or is it done some other way? Thanks in advance. -- David Veeneman Foresight Systems
4
2931
by: Peter Newman | last post by:
the data input app im writing has some 30 + input fields and i want to be able to format them. I know i can use the .validate on each textbox and format the 'string' however this require loads of repetitive codeing. Is there a way possible to globally format the textboxes both on the .validate function, as well as when the enter key is pressed. Any suggestions or examples wouldbe very usefull, as i have a loads of these data forms...
11
5003
by: Dustan | last post by:
Is there any builtin function or module with a function similar to my made-up, not-written deformat function as follows? I can't imagine it would be too easy to write, but possible... 'I am coding, and he coded last week.' ('coding', 'coded', 'week') expanded (for better visual): ('coding', 'coded', 'week')
9
2323
by: john coltrane | last post by:
Is there way to create a formatted string in a similar that is similar to sprintf? The same for printing, printf? C,D,E,F,G,N,X for currency, decimal, exponential, fixed, general, numerical, and hex but these do not seem to allow for specifying the number of decimals, left/right placement, or string formatting. Thanks
6
4607
by: Tomasz J | last post by:
Hello developers, I bind my TextBox control specyfying a format stored in my application global ApplicationContext object - it has a static string CurrencyFormat property. The problem - this works fine: Text='<%# Eval("Price", ApplicationContext.CurrencyFormat) %>'
2
1606
by: Jean-Paul Calderone | last post by:
On Fri, 5 Sep 2008 14:24:16 -0500, Robert Dailey <rcdailey@gmail.comwrote: mystring = ( "This is a very long string that " "spans multiple lines and does " "not include line breaks or tabs " "from the source file between " "the strings partitions.") Jean-Paul
0
9530
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10236
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10182
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10017
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7552
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6793
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.