How to set ‘date only’ or ‘time only’ values in fields through XPages

XPages always save date and time in a field

When a form has the fields with “date only” or “time only”, then the stored data format are different between Notes Client and XPages.

For example, I prepared the field with “date only” named “Date” as following (sorry, screenshots are all Japanese) :

年月日のみをもつフィールド

And also field with “time only” named “Time”:

時刻の表示のみをもつフィールド

Now I saved a new document with this form from Notes Client (9.0.1 FP3), then I got following fields’ data.

Notesクライアントで日付フィールドを保存

Notesクライアントで日付フィールドを保存

As you see, “Date” field has “2015/03/02″, and Time field has”14:35:00”

As the next, I created the simple XPage to save a new document with same form and fields so that I can compare the data between Notes Client and XPages.

XPagesで日付フィールドの保存

Below is the source code for Date field and Time field in XPages.

Date Field

	<xp:inputText value="#{document1.Date}" id="date1">
		<xp:dateTimeHelper id="dateTimeHelper1"></xp:dateTimeHelper>
		<xp:this.converter>
			<xp:convertDateTime type="date" dateStyle="short">
			</xp:convertDateTime>
		</xp:this.converter>
	</xp:inputText>

Time Field

	<xp:inputText id="time1" value="#{document1.Time}">
		<xp:dateTimeHelper id="dateTimeHelper2"></xp:dateTimeHelper>
		<xp:this.converter>
			<xp:convertDateTime type="time" timeStyle="short"
				timeZone="Japan">
			</xp:convertDateTime>
		</xp:this.converter>
	</xp:inputText>

Each inputText specify date, time to the Type attribute of convertDateTime.

After saving a new document from this XPage, below is the screenshot of stored field data:

XPagesから保存された文書のDateフィールド

XPagesから保存された文書のTimeフィールド

So both of fields have “yyyy/MM/dd hh:mm:dd ZZ” format.

Why you need to save “date only” or “time only” data from XPages?

I just don’t post this blog to argue which format is proper way and why XPages behaves differently.

In the real world, I think you or your customers already have the NSF apps and you may want to improve them as the web application by XPages. In that case, especially you decide to keep using the NSF from both Notes Client and XPages, then I recommend to adjust the field format to Notes Client one instead of XPages one so that all existing documents are supported both Notes Client and XPages without changing data.

Use setAnyTime()、setAnyDate() of NotesDateTime class

Maybe there are a lot of way to achieve what I want below. But one of easy way is using NotesDateTime class of SSJS.

	<xp:this.data>
		<xp:dominoDocument var="document1" formName="Form"
			computeWithForm="both">
			<xp:this.postSaveDocument><![CDATA[#{javascript:
var dt:NotesDateTime = document1.getItemValueDateTime("Date");
dt.setAnyTime();
var tm:NotesDateTime = document1.getItemValueDateTime("Time");
tm.setAnyDate();
var notesDoc:NotesDocument = document1.getDocument();
notesDoc.replaceItemValue("Date", dt);
notesDoc.replaceItemValue("Time", tm);
notesDoc.save();
dt.recycle();
tm.recycle();
notesDoc.recycle();
}]]></xp:this.postSaveDocument>
		</xp:dominoDocument>
	</xp:this.data>

The SSJS above converts the DateTime field format by using setAnyTime()、setAnyDate() in PostSaveDocument event after saving the XSP document .

 

About ktatsuki

ケートリック株式会社 代表をしています。 が、根っからのエンジニア脳です。 IBM Notes/Dominoの開発を得意としますが、 C++ / Java / PHP / Javascript などの言語を使ってWEBアプリ、iPhone / Android アプリ開発などをしたりします。 XPagesの仕事をしているとテンションが通常の1.25倍ぐらい高くなります。 I am owner of KTrick Co., Ltd. and Notes/Domino developer. IBM Champion for 2015 - 2017. I am interested in web application development and preferred languages are Notes/Domino, C++ / Java / PHP / Javascript.

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *