1

I want to save the actual date in a variable. only the date, no time

var a = @Date(@Now());
datasource.replaceItemValue("variable", a)`

And

var a = @Date(@Now());
var b = new Date(a.getYear(), a.getMonth(), a.getDay());
datasource.replaceItemValue("variable", b)

Are returning 28.10.14 00:00

var dt:NotesDateTime = @Date(@Now());
datasource.replaceItemValue("variable", dt.getDateOnly());

Is throwing me an error

Isn't there a simple way to get only the actual date without the time?

AstroCB
  • 12,337
  • 20
  • 57
  • 73
  • 2
    Just use the parts you want day, month, and year don't worry about time it will default to 00:00. – brso05 Oct 28 '14 at 13:44
  • the time should not get saved, i want that only "28.10.14" gets stored in the variable –  Oct 28 '14 at 14:14
  • Using DateTime.adjustDay() or the equivalent to increment a date only figure by one day is flawless. Using it to increment a date with 00:00:00 time portion relies on the developer remembering (and being aware of how to successfully) handle daylight savings time. Otherwise it could result in the wrong date being returned. – Paul Stephen Withers Oct 28 '14 at 14:22
  • @brso05 because i don't need the time :) –  Oct 28 '14 at 14:31

3 Answers3

4

Use setAnyTime() metohd of NotesDateTime class to remove time component.

  • Agreed. But... if you try to do `datasource.replaceItemValue("variable", dt);` in SSJS with a data-only DateTime object it doesn't work. It only works if you do `datasource.getDocument().replaceItemValue("variable", dt);` – Mark Leusink Oct 28 '14 at 14:47
  • setAnyTime() is working, but i can't save it to my variable. @Mark it's not saving the date in my variable. i used: `var doc:NotesDocument = currentDocument.getDocument(); var dt:NotesDateTime = doc.getCreated(); dt.setAnyTime(); dt.getLocalTime(); datasource.getDocument().replaceItemValue("variable", dt);` –  Oct 28 '14 at 15:09
  • I'm not sure why you're adding dt.getLocalTime(). Please check the help and usage of that method. It's used to return a String value to a variable. It's not used to modify the underlying DateTime object. – Paul Stephen Withers Oct 28 '14 at 16:27
  • You might have a look here too: http://stackoverflow.com/a/19662232/2065611 . Is currentDocument really different from datasource? If not, you could write `doc.replaceItemValue("variable", dt)` – Knut Herrmann Oct 28 '14 at 16:29
  • `doc.replaceItemValue("variable", dt)` is saving the dt into the variable but with the time. I'm using the code in the `beforePageLoad` and on the page i want to display the date. –  Oct 29 '14 at 08:25
  • i save now the date and the time in the variable and for display i show only the date. –  Oct 29 '14 at 13:56
0

If you want to save only the date use a textfield and convert the text to date, if you need it in your code

PoisonedYouth
  • 548
  • 3
  • 16
  • Just be aware that using a text field will mean it is not usable in a full-text search, e.g. [MyDate] > 28/10/2014 – Paul Stephen Withers Oct 28 '14 at 14:23
  • You're right. But I don't know an other solution to store only date part in an date/time field. It depends on the use of the field value in the application workflow – PoisonedYouth Oct 28 '14 at 14:28
0

@Now uses a java.util.Date, which includes time portions. .getDateOnly() is probably throwing an error because that returns a String.

The underlying session.createDateTime() method accepts either text, a java.util.Date or a java.util.Calendar. I think all of them need to include a time element.

If you're only storing the value for reference, I'd agree with brso05 to not worry.

If you're ever likely to use @Adjust (or an equivalent), then not worrying about the time is a recipe for disaster, because every time you try to adjust, you need to remember to take into account Daylight Savings Time.

One option is to set the time to 12:00 midday. That means DST will not be a problem.

java.sql.Date is specifically designed to only include the Date portion, without a time element. Jesse Gallagher talks about java.sql.Date in the context of his frostillic.us framework https://frostillic.us/f.nsf/posts/32A63DD640D868D885257D18006659A9 and he was the one I found out about java.sql.Date from. I'm not sure how he stores those values though.

I'm not sure if the OpenNTF Domino API will allow you to just pass a java.sql.Date to a field and so store just the date portion.

Paul Stephen Withers
  • 15,699
  • 1
  • 15
  • 33
  • I store it this way: https://github.com/jesse-gallagher/XPages-Scaffolding/blob/master/frostillicus.framework/src/frostillicus/xsp/model/domino/AbstractDominoModel.java#L331 . It ain't pretty, but it works. Basically, if the field is supposed to be date or time only, I store it as date/time and then use `session.evaluate` to fix the stored value. – Jesse Gallagher Oct 28 '14 at 14:58