473,804 Members | 2,109 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
26 2800
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@we b.de> writes:
In contrast, in older UAs like Opera 6 (IIRC), `location' stores
only a primitive string value.
YRI (You Remember Incorrectly :)


Having just installed Opera 6.0 again [1], I realize that you are both
right *and* wrong! :-)

I remembered correctly, *but* my testing method was in fact flawed:
From the fact that Opera's `location' shows no properties when iterating
through them with `for (var p in location)', I falsely concluded that it
has none and is therefore no object at all.[2] But as you wrote, it is of
type `object' and has at least `href', `search' and `protocol' properties
(when I think about it, Opera won't have distributed this much if it had not.)

Because you cannot rely on what iteration shows, is there a way to get
all properties of an object other than brute force, and if that, how it
is done?
In Opera 4, 5 and 6, location is an object.
It is in v6, and most certainly is in v4 and v5.
I don't have Opera 3 installed,
See [1].
The same goes for Netscape 4 and 3.
ACK
> There is some magic to the location object, though, since *assigning*
> to it will really assign to location.href.


There is no magic involved :) See
http://devedge.netscape.com/library/.../location.html


Tha qualified as "magic" to me (something that depends on internal
code and cannot be implemented by a Javscript programmer,


OK. I don't consider things described in specifications "magic".
It says:
---
If you assign a string to the location property of an object,
JavaScript creates a location object and assigns that string to its
href property.
---
That is not correct for *any* object. The following gives me "string":
---
var x = {}; // or var x=document.body ;
x.location = "foo";
typeof x.location
---
So, it is only for window objects, not any object.


It also says:

| The location object is contained by the window object and is within its
| scope. If you refer to a location object without specifying a window, the
| location object represents the current location. If you refer to a
| location object and specify a window name, as in windowReference .location,
| the location object represents the location of the specified window.

and BTW, your `x' is not a Location object.
> As a curiosity, in Opera 7 the location.valueO f method is the same
> as the location.toStri ng.


Same in Mozilla/5.0 and IE 6.0 SP-1, as expected.


In my IE 6, the location object doesn't have a valueOf property.


Oops! Me too. Debug bug. System halted.
PointedEars
___________
[1] <http://arc.opera.com/pub/opera/>
[2] <http://pointedears.de. vu/scripts/test/location.html>

Jul 20 '05 #21
Thomas 'PointedEars' Lahn <Po*********@we b.de> writes:
Because you cannot rely on what iteration shows, is there a way to get
all properties of an object other than brute force, and if that, how it
is done?
No. You can only get the enumerable properties. For the rest, you must
do a brute force search (i.e., you can't in practice).

In Opera, only methods are generally enumerable, and not even all of
them. The enumerable properties of O7's location object are:
assign, reload, replace, toString, and valueOf
As you say, in O6, there are no enumerable properties.
I don't have Opera 3 installed,


See [1].


I have it, it's just not installed right now :)
It also says:

| The location object is contained by the window object and is within its
| scope. If you refer to a location object without specifying a window, the
| location object represents the current location. If you refer to a
| location object and specify a window name, as in windowReference .location,
| the location object represents the location of the specified window.
That just sounds like normal scope rules when the "current" window object
is also the global object of the execution context. Location objects are
just properties of window objects.
and BTW, your `x' is not a Location object.


Nope. But they did say that if you assigned to the location property
of an object, it would create a location object in its place. Apparenty
it is only true for window objects.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #22
JRS: In article <20************ *************** @mb-m20.aol.com>, seen in
news:comp.lang. javascript, HikksNotAtHome <hi************ @aol.com>
posted at Thu, 6 Nov 2003 21:35:14 :-
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.

It is possible that AOL may have problems with it; certainly they
should. If Mr. H Ikksnot gets a couple of AOL accounts, one for use at
work and the other at home, he may be disgruntled to receive your spam.

One should not use an address that may cause mail to be directed to an
account, extant or otherwise, that one does not have positive permission
to use.

Different providers, however, do actually state different, or no,
conditions; presumably depending on a combination of their corporate
intelligence and public-spiritedness.

It is possible that AOL may have created, or be willing to create, some
range or ranges of left parts that will not be allowed for customers and
can therefore be offered for your purpose; they would then probably
arrange to bounce or dump all messages to that range efficiently.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME ©
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demo n.co.uk/news-use.htm> : about usage of News.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Jul 20 '05 #23
In comp.lang.javas cript, Thomas 'PointedEars' Lahn wrote:
My only problem now is how to extract this from the document.locati on
property (it is there). I've tried code the parse the string to write
the last 8 digits, and code to find the "?" and write the remainder.


That has nothing to do with CGI as you have told before!


No, indeed, it was my flawed understanding of the object returned from
the .location property. All sorted now.

Next year I'm going to be teaching a university course that involves JS,
all I have to do is stay one step ahead of the students ;-)
--
Nige

Please replace YYYY with the current year
ille quis mortem cum maximus ludos, vincat
Jul 20 '05 #24
"Lasse Reichstein Nielsen" <lr*@hotpop.com > wrote in message
news:65******** **@hotpop.com.. .
Because you cannot rely on what iteration shows, is there a
way to get all properties of an object other than brute force,
and if that, how it is done?


No. You can only get the enumerable properties. For the rest,
you must do a brute force search (i.e., you can't in practice).

<snip>

Do you really mean you can't in practice? Certainly it would not be
practical to test every combination of character sequence that could be
a property name (the end on the universe would arrive before the
results), but I don't see a problem with trying each item in a list of
likely property names against an object to see which return
non-undefined values. Then the usefulness of the results only depends on
the quality of the list of likely property names. It wouldn't be quick
but it wouldn't be so slow as to be impractical.

Richard.
Jul 20 '05 #25
"Richard Cornford" <Ri*****@litote s.demon.co.uk> writes:
Do you really mean you can't in practice? Certainly it would not be
practical to test every combination of character sequence that could
be a property name (the end on the universe would arrive before the
results),
Find them all, no. It is indeed the heat death of the Universe that
prevents that.
but I don't see a problem with trying each item in a list of likely
property names against an object to see which return non-undefined
values.
Absolutely possible. Later versions of Javascript even has a way
to check whether an object has a property ("foo" in obj).
Then the usefulness of the results only depends on the
quality of the list of likely property names. It wouldn't be quick
but it wouldn't be so slow as to be impractical.


If you know what to look for, it is easier to find it :)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #26
JRS: In article <dt************ *************** *****@4ax.com>, seen in
news:comp.lang. javascript, Nige <uY***@ntlworld .com> posted at Fri, 7
Nov 2003 10:47:29 :-
In comp.lang.javas cript, Thomas 'PointedEars' Lahn wrote:
Time zones don't enter into it, all users are in the UK.

I wonder how you can be sure.


Because it is a campaign site for broadband in Kent.


I am reliably informed that there is a modicum of Kent that keeps Paris
Time. However, they are probably not possible direct customers for the
broadband service. And if they were, they might be customers best
avoided (on mere pragmatic grounds; nothing uncomplimentary ).

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demo n.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jul 20 '05 #27

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
5940
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
3944
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
4028
by: Robert Manookian | last post by:
How do you format strings? i.e. In VB6: Format("AB34567", "@@@@@-@@") = "AB345-67" In .Net: ????????
2
2273
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
2933
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
5006
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
2324
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
4609
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
1608
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
9716
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
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
10604
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10354
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...
0
9177
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6870
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
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
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
3837
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.