How to time duration calculate in react js - javascript

I want to calculate the time duration with the current time and another time. In my problem the current time and all ready time in data base time format are different, this format is given format: 2020-11-07 , 22:52
but now time format is 30/10/2020 , 20:50:34 . So I have a problem with this diffrent format.

You can convert both values to timestamps (in milliseconds) from their own formats and then calculate the difference in milliseconds with simple math.
If you are having trouble parsing the values, you can check Date documentation. An example can be:
let birthday = new Date('1995-12-17T03:24:00')

Related

Simplest way to find out how many milliseconds until a specific time in a specific timezone (taking DST into account)?

I have a piece of code which finds the difference between two dates(in the format of yyyy-MM-dd hh:mm:ss) . This code is run in multiple servers across the globe. One of the two dates is the current time in that particular timezone where the code is being run(server time) and another is the time obtained from a database. if the difference between these two times is greater than 86400 seconds(1day), then it should print "invalid" else, it should print "valid".
Problem im facing with the code is when I run it on my local, its working fine, but when i deploy it onto a server in US, its taking GMT time into consideration and not local time.
Wherever the code is run, I want the difference between current time and time fetched from the database, and if its greater than 86400 seconds, i want to print invalid. How to achieve this in java?
PS: I tried with Date object, but its considering GMT only everywhere.
I would use GMT everywhere and only convert to the local times for display purposes.
To find the difference, convert both times to the same timezone (say GMT) and take the difference.
You can do it by the below example code.
Date date = new Date();
DateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
formatter.setTimeZone(TimeZone.getTimeZone("CET"));
Date date1 = dateformat.parse(formatter.format(date));
// Set the formatter to use a different timezone
formatter.setTimeZone(TimeZone.getTimeZone("IST"));
Date date2 = dateformat.parse(formatter.format(date));
// Prints the date in the IST timezone
// System.out.println(formatter.format(date));
Now compare date1 with date2
First, I concur with Peter Lawrey's answer up there. It is usually good practice to store all time in the database for a single zone, and render it with offset for the user based upon the user's locale.
To find the difference, use the method getTime() to get the time in milliseconds from the epoch for each date. The calculation for the difference of 1 day is then 86400 * 1000 milliseconds. Or, perhaps, store the time in milliseconds from epoch in the database, and use a DB procedure/function at the time of retrieval.
Hope this helps.

How to compare timespan in javascript after timezone conversion using moment js

I've a list of timespan(object list actually), like 2:00, 15:00, 18:00 etc, it is in utc.
Now i want to convert this time slot back to CST and then sort it, as i want my time sorted in cst.
For timezone conversion i needed temporary date. so i choose current utc date by
moment.utc(mytimespan). and performed the timezone conversion by .tz("CST").
So list is converted to 20:00,9:00, 12:00
Here please note that i got 20:00 in first place instead of last place in the list.
This is due to date part of moment which went in back date.
All here i want is my timespan in sorted form without any effect of date.
please me to find a way to do it without string conversion!
Thanks
Update
my currently working code using string conversion
TimeSpanDetails.sort(function compare(a, b) {
return moment(moment.utc(a.startTime).tz("CST").format("HH:mm"),"HH:mm").isAfter(moment(moment.utc(b.startTime).tz("CST").format("HH:mm"),"HH:mm")) ? 1 : -1;
});
Now i want to do it without string conversion using format
A few things:
A "time span" usually refers to a duration of time, not a time-of-day. These are two very different concepts that are sometimes confused. Consider:
A timespan of 99 hours is perfectly valid, but "99:00" is nonsensical as a time-of-day.
Due to daylight saving time and other time zone transitions, a timespan can't necessarily be thought of as "time since midnight" because midnight may or may not exist, or some other hour of the day may be absent or repeated.
Time spans can be negative in some programing languages, usually representing a period before a given point in time.
The tz function in Moment.js takes IANA time zone names. You should not use CT or CST, but rather America/Chicago, for example. However, time zones are completely unrelated to time spans, so you should not be applying them at all. You do not need moment-timezone.
Moment represents time spans in Duration objects. You can parse them from strings like so:
var d = moment.duration('99:00');
Duration objects convert numerically to milliseconds, so they are comparable like so:
var a = moment.duration('00:00');
var b = moment.duration('01:00');
var c = a < b; //=> true
Moment does not have a strongly typed object for a time-of-day, but you can use Moment in UTC mode so that it does not have DST transitions, and then just let it use the current day. HOWEVER:
This would assume that all time-of-day values you have should be evaluated on the same date.
This may or may not be the case.
Consider that if all you have is time-of-day and don't know what dates they're from, then the values ['23:00', '00:00'] may be sorted already and only one hour apart, or perhaps they're out of sequence and they are 23 hours apart.

How the .getTime() can preserve the original date dd/MM/yyyy?

I have a trouble. I have an angular Javascript application that is storing the dates in mysql with milliseconds, but in some moments the date is interpreted with one more or less day. For example:
If the user selects in the date picker: 03/02/2017, the application is saving this (in milliseconds) as 02/02/2017 or 04/02/2017. I believe it is due to the timezone. This is the way I'm using to convert the date in milliseconds:
var temp = $("#datetimeField").val().split("/");
var newDatetime = new Date(temp[2], temp[1] - 1, temp[0]).getTime();
As you can see, I know the day, the month and the year before store it in the database. Normally the date is stored and works well, but in some moment the date changes as I showed above. How can I always get temp[2]/temp[1]-1/temp[0] ??? from the stored milliseconds?

What is a good way to convert %H:%M:%S strings to timespan?

I have a data set with most of the data points in H:M:S format, such as 20:59:59, showing the time span used by individual athletes at different laps.
With D3.js, what is the best way to convert it to time spans, i.e. number of seconds?
I tried parser = d3.timeParse("%H:%M:%S"); but it seems to be very wrong..
If you are using D3 3.x, then you can construct a suitable parser with:
var parser=d3.time.format("%H:%M:%S");
or if you were using D3 4.x:
var parser=d3.timeParse("%H:%M:%S");
Either way, you would then be able to convert your data points into JS Date objects like this:
var t1 = parser.parse("20:59:59");
var t2 = parser.parse("21:02:13");
You can find out the elapsed time between these two time instances by subtracting, eg:
var elapsed = t2 - t1; /* Returns 134000 = 134 seconds */
elapsed will be the number of elapsed milliseconds.
The reason that works is that Date objects provide a valueOf() method which returns the number of milliseconds elapsed since Jan 1 1970. Subtracting the date objects automatically calls valueOf() and subtracts the returned values.
Note that if the time strings you are parsing do not include a date portion, then you will have problems if the starting time and ending time are not in the same day.
EDIT:
From the comments it becomes clear that the times you are working with are already lap durations, in H:M:S format, and you want to convert them to seconds. That does not require D3, it is easy enough:
var components = /^(\d+):(\d+):(\d+)$/.exec("20:59:59");
var elapsedSecs = components[1]*3600+components[2]*60+components[3];
You can use d3.time.format("%H:%M:%S") for your conversion.

Meteor - MongoDb - How to get seconds remaining between current data time and future date time

I want to get seconds remaining between a current dataTime and future dateTime.
I am using Meteor + MongoDb.
In Mongo DataTime is saved like this:
2015-12-11T06:14:39.671Z
I want seconds remaining between current datatime and future or expiry datetime.
keep your time data in epoch format which is basically storing data in milliseconds. then you'll be easy able to compare using momentJS or substracting. And in MongoDB , this data will be stored as Number type not Date
var time= (new Date).getTime();
this piece of code will return time in milliseconds.
The javascript Date can handle the sample input as listed in your question. If that format is consistant, the following should get you your answer:
var difference = (new Date(databaseDate).getTime()-Date.now())/1000;
Where Date.getTime() and Date.now() are in milliseconds (hence the 1000).

Categories