logo

JavaScript @Now Equivalent

Have you ever needed to set a field's value to the current time? Probably. Just put @Now in the Default Value event of the field. But, this is the time that the server thinks it is! What if you want to know the time your user is on.

Consider the notes.net café. When I create a document it says it was at the time the server is on, not me. So it's a lie really. That's not when *I* created it. Would it not be more useful if you knew the time that somebody replied to you in their time? I think so.

The only way to do this is to use Client-Side JavaScript. The following function returns a string in the format:

"dd/mm/yyyy hh:mm:ss"
(or "mm/dd" if it is required in that format).

You can call it from a hyper-link that the user clicks, in the onLoad event of the form or when the user submits the form. It's up to you and your requirements...

<script>
/*
I've used the arguments property of a JavaScript
function, not because I had to, but just to so as to
show you all how. The arguments[] are:
[0] DateFormat = Whether it is mm/dd or dd/mm (string)
[1] TargetField = Where to put the returned value (object)
*/
function getDateTimeNow(){
nw = new Date(); //Now !
mt = nw.getMonth() +1; //getMonth() is zero based !
mt = (mt < 10) ? "0" + mt : mt;
dy = nw.getDate();
dy = (dy < 10) ? "0" + dy : dy;
yr = nw.getYear();
hr = nw.getHours();
hr = (hr < 10) ? "0" + hr : hr;
mn = nw.getMinutes();
mn = (mn < 10) ? "0" + mn : mn;
sc = nw.getSeconds();
sc = (sc < 10) ? "0" + sc : sc;

ret = (arguments[0].toLowerCase().charAt(0) == "d") ? dy +"/"
+ mt : mt + "/" + dy;
ret += "/" + yr + " " + hr + ":" + mn + ":" + sc;

arguments[1].value = ret;
}</script>


Test it out here:


Set Date/Time (dd/mm)
Set Date/Time (mm/dd)


It is important to get the "dd/mm" format correct so as to avoid errors/confusion. If you are NOT 100% percent sure what it will be then use this method to find out. The function call would then look something like:

<a href="JavaScript:getDateTimeNow(document.forms[0].DateFormat.value , document.forms[0].NowDateTime);">Set Date</a>




Note: If you know a bit of JavaScript you may be wondering why I'm not just using built in calls like toLocaleString(). Having tried these I've found that what they return depends on OS and browser type/version. This method will always return dates in the format that the Notes server expects.

Feedback

  1. Relative time

    The only problem is that the user's actual time is meaningfull only to the creator.

    The server time is kind of like "Greeenwich mean time" - a common, consistent time reference across all documents on the server. That can be used to tell how much time is between 2 document creations.

      • avatar
      • Darby LaPorte-Magill
      • Thu 15 Aug 2002

      Re: Relative time

      Jason has a good point. How could we show the time zone as well?

      I wrote this:

      function getTimeZone(){ var d = new Date(); var intTimeZone = d.getTimezoneOffset() / 60; //returns minutes, putting into hours switch (intTimeZone) { case 7: return "PST"; default: return ""; } }

      but it's not right. I am in California, so date.getTimezoneOffset() should return 8 for me but it returns 7. Help!

      Also, I guess I'll have to create dsp fields to show the timezone for each field (dateCreated field and dateLastEdited field)

    • avatar
    • James
    • Thu 10 Mar 2005

    error

    Think you will find there is a Javascript error on your page when you try and test it

      • avatar
      • Jake Howlett
      • Thu 10 Mar 2005

      Re: error

      Fixed. Thanks James.

      V6 of this site does away with Domino's default form. Trouble is some of my articles rely on it. Doh!

      Jake

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment:



Navigate other articles in the category "JavaScript"

« Previous Article Next Article »
Binding events to objects   A Radio Button Quiz

About This Article

Author: Jake Howlett
Category: JavaScript
Keywords: date; time;

Options

Feedback
Print Friendly

Let's Get Social


About This Website

CodeStore is all about web development. Concentrating on Lotus Domino, ASP.NET, Flex, SharePoint and all things internet.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »