Related
I need to send via AJAX a date for a delphi server that accept the date in float format and set a TDateTime property.
Eg.
var
date: TDateTime;
begin
date := StrToFloat(Request.QueryFields.Values['date']);
end;
Delphi TDateTime start from 30/12/1989, i have tested with:
var
date: TDateTime;
begin
date := StrToFloat('0');
ShowMessage( DateTimeToStr(date) ); // show: 30/12/1899
end;
JavaScript Date start from unix epoch, i have tested with:
console.log(new Date(0)); // Thu Jan 01 1970 01:00:00 GMT+0100
a simple conversion seem subtract the difference, but doesn't works, eg.:
// javascipt
var delphiTime = (new Date("07-24-2019") - new Date("12-30-1899")) / 1000;
console.log(delphiTime ); // 3773084400
// delphi
ShowMessage( DateTimeToStr(3773084400) ); // show 00/00/00
the strange fact, on delphi, Now is 43670.654378:
ShowMessage( FloatToStr(Now) ); // 43670.654378
In delphi 0 is 1899 and 43670 is 2019...
How works the date format in Delphi and how convert a unix date to delphi date with math?
Side note: i can't modify the server, i need to solve the issue client side with javascript
UPDATE:
In Delphi the float value = 1 would be 31.12.1899, 2 = 01.01.1900 and so on. Each unit seem a day.
function jsDateToDelphiDate(date){
const seconds = (new Date(date).getTime() - new Date("12-30-1899").getTime()) / 1000;
const days = seconds / 60 / 60 / 24;
return days;
}
console.log(jsDateToDelphiDate(new Date("07-24-2019 16:00:00"))); // 43670.625
43670.625 on delphi is 23/07/2019 15:00.
Why i lose 1 hour?
How works the date format in Delphi
This is fully documented on Embarcadero's DocWiki:
System.TDateTime
The TDateTime class inherits a val data member--declared as a double--that holds the date-time value. The integral part of a TDateTime value is the number of days that have passed since December 30, 1899. The fractional part of a TDateTime value is the time of day.
...
The following table displays examples of TDateTime values and their corresponding dates and times:
Value Description
0 December 30, 1899; 12:00 A.M.
2.75 January 1, 1900; 6:00 P.M.
-1.25 December 29, 1899; 6:00 A.M.
35065 January 1, 1996; 12:00 A.M.
how convert a unix date to delphi date with math?
Side note: i can't modify the server, i need to solve the issue client side with javascript
A Unix date/time is represented as the number of seconds since January 1 1970 00:00:00 UTC. Delphi has a UnixDateDelta constant in the SysUtils unit which is defined as 25569, the number of days from December 31 1899 to January 1 1970. So, a TDateTime value of 25569.0 exactly represents January 1 1970 00:00:00 (UTC vs local is up to you to decide when creating a TDateTime). You can then add seconds to that value to get the final TDateTime value for any Unix date/time.
In a TDateTime, you can add whole days to the integral portion (ie, Unix + 1 day = 25569.0 + 1 = 25570.0), but adding seconds within a day is slightly more work, as seconds are not represented as-is in TDateTime, as you can see in the table above. 0.25 is 6:00 AM (21600 seconds after midnight) and 0.75 is 6:00 PM (64800 seconds after midnight). So seconds are represented in TDateTime as a fraction with 86400 (the number of seconds in a day) as the denominator.
A JavaScript Date object is represented as the number of milliseconds since midnight on January 1 1970. You can divide a Date value by 1000 to get whole seconds, and divide that value by 86400 to get whole days and fractional seconds, which you can then add to 25569.0 to produce a TDateTime value.
function jsDateToDelphiDate(dateToConvert){
const UnixDateDelta = 25569.0;
const SecsPerDay = 86400;
const MSecsPerSec = 1000;
var UnixSeconds = dateToConvert.getTime() / MSecsPerSec; // 1563984000
var SecsToAdd = UnixSeconds / SecsPerDay; // 18101.666666666668
return UnixDateDelta + SecsToAdd;
}
// don't forget to force UTC, or else the Date value
// will be skewed by the local timezone offset...
console.log(jsDateToDelphiDate(new Date("2019-07-24T16:00:00Z"))); // 43670.66666666667
console.log(jsDateToDelphiDate(new Date(Date.UTC(2019, 6, 24, 16, 0, 0)))); // 43670.66666666667
Delphi has a UnixToDateTime() function in the DateUtils unit which performs this calculation for you. So, if you can change your AJAX code to pass a Unix timestamp as-is to Delphi, you can let Delphi calculate a suitable TDateTime.
Note, in this example, the resulting TDateTime value is in UTC. After transmitting the value via AJAX to Delphi, if your Delphi code needs a TDateTime in local time, that is a simple calculation to adjust the TDateTime based on the local machine's timezone offset in minutes, which you can get using platform APIs, such as GetTimeZoneInformation() on Windows, etc. Delphi has an IncMinute() function in the DateUtils unit that you can use for that adjustment.
In JS, valueOf Date is in milliseconds. If you want to convert it to days, simply divide it by 24*60*60*1000.
To convert the return value of new Date().valueOf() to Delphi TDateTime you can use this function:
function jsDateToDelphiDate(millis: int64): TDateTime;
begin
var UnixSeconds: Integer := Round(millis / MSecsPerSec);
Result := UnixToDateTime(UnixSeconds, False);
end;
I need to have a function where the user can insert a certain timeframe (e.g. 1 week or 5 days and 12 hours). Duration from Moment.js looked the most promising.
The code below returns 2 00:00, where 2 equals the numbers of days. This should be 1 because there are only 24 hours in there.
moment.utc(moment.duration(24, 'hours').as("milliseconds")).format("D HH:mm");
What am I doing wrong here?
You are setting 24 hours as a millisecond offset from 1970-01-01 (Unix epoch) by calling moment.utc(...). This means that your moment is holding the date 1970-01-02 00:00 and you are then printing the day part.
If the time frames are fixed and set by yourself you could always manually put in the amount of time in milliseconds to start with. e.g. 24 hours is 86400000 milliseconds.
You have to format the milliseconds in moment duration not in moment. I think the below line gives your expected value.
moment.duration(moment.duration(24, 'hours').as("milliseconds")).format("D HH:mm");
Answer : 1 00:00
I got it fixed with the following code and the plugin:
moment.duration(moment().diff(moment().subtract(1, 'days')));
i have a datepicker and some time slots in my view. the time slots are checkboxes and the value of checkbox should be a timestamp which i aim to get by combining the value from datepicker and the checkbox slot that was checked by the user. Is there any way to do this using javascript of jquery.
Example:
$('#inline_datepicker').datepicker("getDate") + "02:30PM" => create timestamp
I'm not clear on exactly what you're looking for here, but to just get a basic timestamp in string format using milliseconds (from Jan. 1, 1970 at midnight, as is the standard) you can just do this using vanilla javascript:
var timestamp = new Date(2014,1,3,18).getTime();
The format is yyyy/mm/dd/hh and it is zero based and the hours use a 24 hour clock, also called military time if you are from the United States. The above example gives the timestamp for February 4, 2014 at 6pm.
You can then always add time to the time stamp by adding time to that value, too. 1 day is 24 hours, one hour is 60 minutes, one minute is 60 seconds, and one second is 1000 milliseconds. That means after creating a timestamp, if you wanted to add 4 days, 13 hours, 49 minutes, and 6 seconds, you could do this:
var timestamp = new Date(2014,1,3,18).getTime();
timestamp += (((4*24+13)*60+49)*60+6)*1000;
Finally, you can always convert back to a human-readable timestamp using the .toUTCString() method in conjunction with creating a new Date object. The final code would read as such:
var timestamp= new Date(2014,1,3,18).getTime();
console.log(new Date(timestamp).toUTCString());
/* Outputs "Sun, 04 Feb 2014 18:00:00 GMT" */
timestamp += (((4*24+13)*60+49)*60+6)*1000;
console.log(new Date(timestamp).toUTCString());
/* Outputs "Sun, 09 Feb 2014 07:49:06 GMT" */
Try
var date = $('#inline_datepicker').datepicker("getDate");
//24 hour clock
date.setHours(14, 30);
console.log(date)
Demo: Fiddle
To get the time in milliseconds use date.getTime()
How we convert Bright Cove video duration to HH:MM:SSS in Javascript?
Brightcove's API returns the duration in milliseconds. Divide by 1000 then apply one of the answers for formatting seconds as HH:MM:SS e.g. JavaScript seconds to time string with format hh:mm:ss
I am trying to keep track of the days since the birth of my program in epoch days. So, I I give my program:
epochProgram = 15622 // epoch day number that this program was born.
I then get the current time and divide by 1000 to make it seconds. Then I take that and divide it by the number of seconds per a day which is 86400 to convert it to the number of days today since epoch. I then subtract the program epoch birthday number from today's epoch number to see how many days have lapse since the birth of the program.
dateObj = new Date();
var biz = parseInt(dateObj.getTime()/1000));
biz = biz/86400-epochProgram;
Lets say a few days have past and biz=6.30. My issue is this:
12:00 am is at 6.30, at 5:00PM biz=7.0, and at 11:PM, biz=7.2.
Why does the tenths .# digit work as .3 is the start of the say and .2 is the end of the day? What could I do to fix this so I can have a correct day increment?
PS: this is local Pacific time.
Subtract the timezone offset:
var biz = (dateObj.getTime() - dateObj.getTimezoneOffset() * 6e4) / 1000 >>> 0;