ASP: convert milliseconds to date - date

I need to convert a field with milliseconds created by PHP app to a date value. Is there a way to do this using VBScript? or convert to datetime in SQL 2005?
Example: 1113192000 to mm/dd/yyyy hh:mm:ss

Something like the following should work:
Function ConvertPhpToDate(numSeconds)
Dim dEpoch
dEpoch = DateSerial(1970,1,1)
ConvertPhpToDate = DateAdd("s",numSeconds,dEpoch)
End Function
Note, the php time() function returns the number of 'seconds', not milliseconds. http://php.net/manual/en/function.time.php

I think what you are referring to as milliseconds is really the epoch time as returned by the php's time/date functions. You can give this a function a shot to get the epoch time converted to datetime format in ASP:
function epoch2date(myEpoch)
epoch2date = DateAdd("s", myEpoch, "01/01/1970 00:00:00")
end function
Source: http://www.epochconverter.com/epoch/functions.php#asp

msValue = 32312312
dtValue = DateAdd("s", msValue/1000, CDate("1970-01-01 00:00:00"))
Wrap it in a function:
Function TimestampToDate(timestamp)
TimestampToDate = DateAdd("s", timestamp/1000, CDate("1970-01-01 00:00:00"))
End Function

You'll need to have a base time to count the milliseconds from e.g. 1st Jan 1970 or similar.
You then divide the number of milliseconds by 1000 to get the number of seconds - saving the remainder.
Divide the number of seconds by 60 (saving the remainder) to get the number of minutes.
Then 60 again for hours, 24 for days.
Then it get's difficult as you've got leap years to consider. There is another question on this here.
Once you've got your years, days, hours, minutes, seconds and milliseconds you add this to the base date-time to get the date-time represented.
Others have posted code etc. that you might use.

Related

millisecondSinceEpoch returning weird value when parsing date around 1969

I don't know if this is a bug or if I'm using this method incorrectly.
Can someone explain to me why I got a weird value when parsing this date?
This is the minimal script:
void main(){
DateTime zero = DateTime.fromMillisecondsSinceEpoch(0);
print(zero.millisecondsSinceEpoch);
DateTime errorDate2 = DateTime(1969,1,1);
print(errorDate2.millisecondsSinceEpoch);
DateTime errorDate = DateTime(1969,10,2);
print(errorDate.millisecondsSinceEpoch);
}
Based on my sample, the result seems fine when I parse dates bigger than 0 milliseconds since the epoch.
But it gets weird when I parse some date less than 0 milliseconds since the epoch.
From that script, I got this output
0
-31536000000
-7862400000
I parse that output to this website.
In the first and second results, the results are fine, but in the third result, I got a date around 1720.
Can someone explain to me what happens to that function? Did I use it wrong?
What should I do when I want to parse a date less than 0 milliseconds since the epoch?
In a computing context, an epoch is the date and time relative to which a computer's clock and timestamp values are determined. The epoch traditionally corresponds to 0 hours, 0 minutes, and 0 seconds (00:00:00) Coordinated Universal Time (UTC) on a specific date, which varies from system to system. Most versions of Unix, for example, use January 1, 1970 as the epoch date; Windows uses January 1, 1601; Macintosh systems use January 1, 1904, and Digital Equipment Corporation's Virtual Memory System (VMS) uses November 17, 1858.
So here epoch time starts on January 1, 1970, so it will give you a minus value for milliseconds.
You can still parse the date. it will gave just minus value nothing else.
void main() {
DateTime zero = DateTime.fromMillisecondsSinceEpoch(0);
print(zero.millisecondsSinceEpoch);
DateTime errorDate2 = DateTime(1969,1,1);
print(errorDate2.millisecondsSinceEpoch);
DateTime errorDate = DateTime(1969,10,2);
print(errorDate.millisecondsSinceEpoch);
DateTime error3 = DateTime.fromMillisecondsSinceEpoch(errorDate.millisecondsSinceEpoch);
print(error3);
}
Result is
0
-31555800000
-7882200000
1969-10-02 00:00:00.000
It seems that the bug comes from the website that I refer to it.
Thanks! #jamesdlin

Calculate the time difference

I am trying to calculate time difference between 2 ZonedTime dates in Scala. I am receiving dates in "2021-03-19T15:39:42.834248-07:00" format as a String. I need the difference in seconds between 2 dates in Scala. How to convert the string to zoned time and calculate the difference?
You'll want to use the between() method as offered on a temporal.ChronoUnit.
import java.time.ZonedDateTime
import java.time.temporal.ChronoUnit.SECONDS
val start = ZonedDateTime.parse("2021-03-19T15:39:42.834248-07:00")
val stop = ZonedDateTime.parse("2021-03-19T15:49:42.834248-08:00")
val secsBetween:Long = SECONDS.between(start, stop) // 4200
An alternative is to use the until() method on the ZonedDateTime instance itself.
val secsBetween:Long = start.until(stop, SECONDS) //same result
[Java syntax, not Scala.]
tl;dr
Duration
.between
(
OffsetDateTime.parse( "2021-03-19T15:39:42.834248-07:00" ) ,
OffsetDateTime.parse( "2021-03-19T15:49:42.834248-08:00" )
)
.toString()
See this code run live at IdeOne.com.
PT1H10M
…which in standard ISO 8601 format means 1 hour and 10 minutes.
Details
The Answer by jwvh is close, but I would change a couple things.
OffsetDateTime, not ZonedDateTime
Your input strings have only a mere offset-from-UTC but no time zone. So parse those as OffsetDateTime.
An offset is simply a number of hours-minutes-seconds ahead or behind the baseline of UTC, the line drawn through Royal Observatory, Greenwich. An example of an offset is -07:00 which means seven hours behind UTC.
A time zone is much more. A time zone is history of the past, present, and future changes to the offset used by the people of a particular region. A time zone has a name in format of Continent/Region. Given our example above, on some dates, several time zones may share the offset of -07:00, including America/Dawson, America/Los_Angeles, America/Phoenix, America/Boise, and more.
OffsetDateTime odt = OffsetDateTime.parse( "2021-03-19T15:39:42.834248-07:00" ) ;
Duration
Represent a span-of-time using Duration, on the scale of hours-minutes-seconds-nanos.
Duration d = Duration.between( sooner , later ) ;
Generate text in standard ISO 8601 format.
String output = d.toString() ;

Rexx: increment current time value

Hi I'm running REXX script on ZOC terminal and i want to display current time and ETA like this:
start time 22:44:24
end time 22:56:24
but I don't know how to increment current time ???
maybe to convert time to seconds then increment it and then convert time in seconds back to hh:mm:ss ??
I tried this way but dont know how to convert back time from seconds
intTime= TIME('S')+900
say="start time " TIME()
say="end time " intTime
One way would be along the lines of:-
intTime = TIME('S') + 900
hours = (intTime % 3600) // 24
minutes = (intTime // 3600) % 60
seconds = intTime // 60
endtime = RIGHT(hours,2,'0') || ":" || RIGHT(minutes,2,'0') || ":" || RIGHT(seconds,2,'0')
NOTE!! I don't have access to test this and it's been many years since I've written Rexx or had access. However, I think the basic process would work. That is:-
1) Extract the hours as an integer from the time (catering for the the potential to cross into the next day or days ie the // 24 ()).
2) Extract the minutes, as an integer, from the time, after dropping/subtracting the hours (the remainder of the time divided by hours ie intTime // 3600).
3) Extract the seconds, as an integer, from the time. By obtaining the remaining of diving the time by 60 (will drop hours and minutes).
4) Building the end string as a concatenation of the hours, minutes and seconds. With : as the separator between two values (or surrounding the middle values). The right function to include a leading zero.
You could also try:-
intTime = TIME('S',TIME('S')+900,'S')
That is based upon TIME, which may be Object Rexx. I did also read something mentioning an extended TIME/DATE functionality. However, again that may have been referencing Object Rexx. Although, Mike Colishaw's name was mentioned.
Mike Colishaw, I believe, being the creator of the Rexx programming language.

Is there a class in JDK to represent an hour of the day, but not necessarily a specific hour at a specific date?

Is there a class in JDK or Guava to represent an hour of the day, but not necessarily a specific hour at a specific date?
If not, why?
In JDK 1.3-1.7, the answer is no. A specific time within a day is much easier to calculate then date, because you don't have to deal with leap year, leap month, such headache stuff. A simple integer is just enough. When you need to convert the time to a locale string, using SimpleDateFormatter or whatever, you can simply convert the time to a Date, just ignore the date part:
int time = 8 * 60 + 34; // 8:34 am
Date date = new Date(60000L * time);
Reset the time zone to +0, and pass the date to the formatter:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT+0"));
sdf.format(date);
You could simply wrap a byte into a class and every time that the current hour passes 23 within your increment() (or appropriate name) method, set the value of the byte to 0, and whenever the value passes below 0 in your decrement() (or appropriate name) method, set the value of the byte to 23.
As far as I know, there is not a specific class representing Hour (in the JDK or Guava), but there are easy to use classes to fetch the hours from a specific instance of time (which is what I am assuming you are after with this question).
You could use JODA-Time, as Paŭlo Ebermann mentions, but that is an external library. Within the JDK, there is a class called Calendar, which has many useful methods.
To get the hour of a long representing the current time, you could do this:
Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
int hour = c.get(Calendar.HOUR); //returns 0-11
int hourOfDay = c.get(Calendar.HOUR_OF_DAY); //returns 0-23

iPHONE SDK - Subtracting a time given another time

having trouble with this. i need to come up with a logic code.
How do i, given 2 set of times. subtract one from the other and change it all to seconds.
Lets say i have '12:00:00' and '15:00:00'
Now i want to subtract 12:00:00 from 15:00:00 so the result will be 03:00:00. Then change it all to seconds so that will be 10800 seconds
i already have two sets of time in an NSString after being Formatted with a NSDateFormatter
This is what you need to look for : NSDate -(NSTimeInterval)timeIntervalSinceDate
Convert your string to an NSDate and call this method [thisDate timeIntervalSinceDate:thatDate] will return you an NSTimeInterval, type double