Date/Time Value from Milliseconds using LotusScript Only

Hi, I have a date/time value in milliseconds which I need to convert to a readable value in LotusScript.

I can do it in Java and JavaScript but not in LS (please don’t suggest using - Uselsx “*javacon” need a LS function only).

Have an example of how to do it in JavaScript below. Thanks

example:

var millsectime = 1194841508781;

var currentTime = new Date(millsectime);

var month = currentTime.getMonth() + 1;

var day = currentTime.getDate();

var year = currentTime.getFullYear();

var weekDay = currentTime.getDay();

var second = currentTime.getSeconds();

var minute = currentTime.getMinutes();

var hour = currentTime.getHours();

alert(hour + “:” + minute + “:” + second + " " + day + “/” + month + “/” + year);

Subject: Date/Time Value from Milliseconds using LotusScript Only

Hi

You can use Evaluate(@Adjust) in LS since Adjust in LS can accept only integers for adjusting seconds so use evaluate for @Adjust and convert ur unix time. If you still have problem then i can send you the code.

Regards

Fraser

Subject: RE: Date/Time Value from Milliseconds using LotusScript Only

Nonsense. Solving the problem is simply a matter of knowing what you’re dealing with. The “milliseconds” the original poster is talking about is relative to something: start of day (midnight) of January 1, 1970, and at least in the case of JavaScript, that epoch time is GMT.

LotusScript Date variants are just Doubles with an “I’m a date-time” flag set. The number stored is the number of days and fraction of a day since the LS/VB epoch, which is midnight (start of day), December 31, 1899. The integer portion of the number is the number of days, and the decimal is the time represented as a fraction of the day.

One merely needs to create the JS epoch as a Date Variant, then add the number of days the millisecond value represents. There is one tiny little hitch in the plan, though, and that’s that a Date Variant is blind when it comes to time zones. A NotesDateTime object is not better – you can read a GMT time, but you can’t write one. However, you can READ a time zone from a NotesDateTime.

Function JSMillisecondsToLSDate(millis As Double) As Variant

Dim ndt As NotesDateTime

Dim zoneOffset As Integer

Dim jsEpochDouble As Double, adjustedEpochDouble As Double, millisDateDouble As Double

%REM

JavaScript millisecond values are based on GMT

but writable LotusScript date/time values are local.

We need to know the local timezone offset from GMT,

and for that we need a NotesDateTime object

with both date and time components

%END REM

Set ndt = New NotesDateTime(Now)

zoneOffset = ndt.TimeZone

'The JavaScript epoch is midnight (day start) January 1, 1970 GMT

jsEpochDouble = Cdbl(Datenumber(1970,1,1))

'Adjust epoch to local time

adjustedEpochDouble = jsEpochDouble - (zoneOffset/24)

'There are 86400000 milliseconds in a day

millisDateDouble = adjustedEpochDouble + (millis / 86400000)

JSMillisecondsToLSDate = Cdat(millisDateDouble)

End Function

Getting the returned value as text is easily done to whatever spec you need using the LS Format function.

Subject: RE: Date/Time Value from Milliseconds using LotusScript Only

stan rogers

I understand what original poster has said. even this code can solve the prob.

notestime=Evaluate(“@Adjust(@Time(1970;1;1;0;0;0);0;0;0;0;0;”+unixTimeInSeconds+“;[INLOCALTIME])”)

Dim dateTime As New NotesDateTime(Cdat(notestime(0))+“GMT”)

dateTime.LocalTime

Do not think only your code will do the magic.

Subject: RE: Date/Time Value from Milliseconds using LotusScript Only

I don’t remember saying that what I posted was the only solution – unlike the assertion in your original posting that one NEEDS to use Evaluate (and that Formula Language would somehow be able to use milliseconds directly). In other words, t’was yourself said there was only one way to do the magic, not I.

Note that a NotesDateTime can also use Adjust values – the restriction being that the values need to be Integers, so one would need to limit adjustments to 32767 units at a time. Doing the “adjust” as a simple addition avoids the loop, but one could as easily do this:

Function JSMillisecondsToLSDate_2 (milliseconds As Double) As Variant

Dim ndt As NotesDateTime

Dim epoch As Variant

Dim seconds As Long

Dim MAX_INTEGER As Integer

Dim daysAdjust As Double, secondsPerDay As Long

Dim secondsAdjust As Double, secondsAdjust As Integer



MAX_INTEGER = 32767

seconds = milliseconds / 1000

epoch = Datenumber(1970,1,1)

Set ndt = New NotesDateTime(Format$(epoch,"General Date") + " " + Format$(epoch, "Long Time") + " GMT")

secondsPerDay = 86400

daysAdjust = Int(seconds / secondsPerDay)

secondsAdjustDbl = seconds - (daysAdjust * 86400)

While (daysAdjust > MAX_INTEGER)

	Call ndt.AdjustDay(MAX_INTEGER)

	daysAdjust = daysAdjust - MAX_INTEGER

Wend

Call ndt.AdjustDay(daysAdjust)

While (secondsAdjustDbl > MAX_INTEGER)

	Call ndt.AdjustSecond(MAX_INTEGER)

	secondsAdjustDbl = secondsAdjustDbl - MAX_INTEGER

Wend

Call ndt.AdjustSecond(secondsAdjustDbl)

JSMillisecondsToLSDate_2 = ndt.LSLocalTime

End Function

Note that secondsAdjust and daysAdjust are declared as Double. This is to avoid an overflow error that should not occur, but does in fact pop up in testing.

Still, there’s no need to go to Formula Language – and Formula Language still won’t let you use milliseconds directly (as you suggested it cold in your original posting). That was the nonsense I was talking about.

As I said, it’s simply a matter of understanding the question and using what’s available. In Notes, there’s almost always more than one way to do things.

Subject: RE: Date/Time Value from Milliseconds using LotusScript Only

Excellent just what I needed. Thanks.