Laravel & Javascript - Converting Time to Todays Date + Time - javascript

Im looking for help converting a Time only feild to dateTime so i can calculate "Time Elapsed"
For Sports events, I have a feild for Date & Time for the event/game, a feild for 1st half start time, and 2nd half start time, these last two do not include dates,
Example,
Event Date - Wed Apr 28 2021 21:29:05 GMT+1200
1st Half - 21:17:22
How can i calculate the time elapsed from the current time to the start time of the 1st half?
Thanks in advance

Related

new Date(...).toISOString() in JavaScript gives incorrect result on one day of the year

I'm building a calendar and I noticed that the date is incorrect on one day of the year.
I wanted to standardize dates to ISO 8601. Let's take 2022-03-27 for example:
const foo = new Date(2022, 2, 27).toISOString()
This gives 2022-03-27T00:00:00.000Z as one would expect.
However, the next day, 2022-03-28:
const bar = new Date(2022, 2, 28).toISOString()
This gives 2022-03-27T23:00:00.000Z - 27th March at 23:00.
Why does this happen?
What's happening is that British Summer Time (Daylight Saving Time, aka DST) is kicking in between your two examples (specifically, it starts at 01:00 on 2022-03-27). toISOString always gives you UTC time (GMT) as you can tell from the Z at the end of the string, but your local time is an hour ahead of UTC after 01:00 on 2022-03-27 until DST ends at 02:00 on 30 October 2022.
In your first example, you're creating a Date with the local time of midnight on 2022-03-27, when your local time is (apparently) GMT+00:00 (like mine here in the UK). So toISOString gives you back 00:00 because your local time is GMT/UTC (that date/time is an hour before DST kicks in).
But in your second example, you're creating a Date in local time at midnight on 2022-03-28, when you're on DST. So you're offset from UTC by an hour (UTC is one hour behind you). Midnight 2022-03-28 UK time is 23:00 2022-03-27 UTC.
I don't know what you want to do with this stuff, but if you want to create a Date for midnight UTC on 2022-03-28, use new Date(Date.UTC(2022, 2, 28)).

Calculate duration of two dates, first one is utc date, second is browser's local gmt time (London timezone)

I have an issue with the calculation of duration between two dates, one is UTC London time, and the second one is the browser's local time.
first date is 2022-04-20T08:29:55.614 (server's UTC time)
second one is 2022-04-20T09:44:16.234 (browser's local time)
I calculate the difference by using the moment:
const differenceDate = moment.duration(currentDate.diff(startupDateTime))
The difference should be 14 minutes, but the result is 1 hour and 14 minutes.
I tried to use the moment-timezone library but the result was almost the same.

Javascript date : go back to the last week number of year_n when on week number 1 of year_n+1

Initially, I got the number of week this week of month of 2020 which is week 43 (Oct 19 2020 - Oct 25 2020).
I also have two buttons which serve as next and previous.
When I click next button, the date changes to (Oct 26 2020, Nov 1 2020) as well as the week number to 44.
The same way goes to previous button. All is working almost fine, However, when I reach year 2021, the week number becomes 1 of course. and when I click previous again, it goes to the last week number of year 2021 instead of 2020.
My code for incrementing and decrementing weekly is this:
date.setDate(date.getDate()+7) //increment
date.setDate(date.getDate()-7) //decrement
I know the reason why it does not go back to the last week of 2020 because I only deducted 7 days.
However, I can't figure out how to go back to the last week of 2020 when I click previous.
Any idea?
UPDATE: Just now, I noticed, when I'm on the last week of 2020 and click next, Instead of going to '2021', It jumps to '2022'.. damn, I really hate working with dates on javascript.
I concluded. using native javascript date is such a hassle,
instead, I used momentjs.
all of the long code simplified by using
moment(date).add/subtract(no_of_days, 'd');
It automatically do the thing for you. no need to get the number of week and the year.

How to convert current time to CET in Angular

I have seen couple of questions on the same, but which I'm facing the issue is not addressed anywhere so that only asking again.
I am trying to convert current time to CET time so initially i had implemented like this
component.ts
public currentDate;
ngOnInit(){
this.currentDate = new Date();
}
component.html
TIME: {{currentDate | date:'h:mm a':'+0100'}}
With above snippet initially it was matching fine with CET time, but now getting one hour difference. And i found in docs like Central European Time CET alternates between UTC+1 (standard time) and UTC+2 (when daylight saving time (DST) is observed).
All countries in the CET time zone observe DST (UTC+2) from 02:00 am on the last Sunday of March until 03:00 am on the last Sunday of October.
So based on this from last Sunday of march to last Sunday of October supposed to add +2hrs..And tried the same and now matching the same with CET.
But how can we handle this dynamically, or any other approach to convert current time to CET, Instead of changing +1hr some time and +2hr some time period.
Thanks in advance.

Calculating Date in JavaScript

I am currently looking to calculate a custom date in JavaScript and my head is beginning to hurt thinking about it. I have a countdown clock that is to start every other Tuesday at 12pm. I have the countdown function working properly using the jQuery countdown plugin by Keith Wood but need assistance in calculating every other Tuesday of the month and having it reset on this day.
All help is greatly appreciated as always.
Thansk in advance
I've had to do something similar (not in JS but the algorithm is similar enough)
Now, before i start, to clarify i'm assuming this is something that happens fortnightly regardless of the length of the month, and not on the second and 4th Tuesday regardless of when it last happened, which is simpler to solve
Pick a date in the past that this event has occured on (or the date of the first occurrence) , we'll call this date base in the following code
var base=new Date('date of first occurrence');
var one_day=1000*60*60*24; //length of day in ms
// assume we care about if the countdown should start today
// this may be different if you are building a calendar etc.
var date_to_check=new Date();
var diff_in_days=math.floor(date_to_check-base)/one_day);
var days_since_last_reset= diff_in_days%14;
if(days_since_last_reset == 0){
//date_to_check is the same day in the fortnightly cycle as base
//i.e. today at some point is when you'll want to show the timer
//If you only want to show the timer between certain times,
//add another check here
}else{
//next reset in (14 - days_since_last_reset) days from date_to_check
}
Or the code-golf-esque version:
if( Math.floor((new Date()-new Date('date of first occurrence'))/1000/60/60/24)%14 == 0 )
//reset/start timer
Please find attached link for Date Library to get the custom calculation date and time functions.
To use it client side, download index.js and assertHelper.js and include that in your HTML.
<script src="assertHelper.js"></script>
<script type="text/javascript" src="index.js"></script>
$( document ).ready(function() {
DateLibrary.getDayOfWeek(new Date("2015-06-15"),{operationType:"Day_of_Week"}); // Output : Monday
}
You can use different functions as given in examples to get custom dates.
To get First Day of quarter From Given Date
DateLibrary.getRelativeDate(new Date("2015-06-15"),
{operationType:"First_Date",granularityType:"Quarters"}) // Output : Wed Apr 01 2015 00:00:00
If first day of week is Sunday, what date will be on Wednesday, if
given date is 15th June 2015
DateLibrary.getRelativeDate(iDate,
{operationType: "Date_of_Weekday_in_Week",
startDayOfWeek:"Sunday",returnDayOfWeek:"Wednesday"}) // Output : Wed Jun 17 2015 00:00:00
If first day of week is Friday, what date will be on Tuesday of 3rd
Week of 2nd month of 3rd quarter of year containing 15th June 2015 as
one of the date.
DateLibrary.getRelativeDate(new Date("2015-06-15"),
{operationType: "Date_of_Weekday_in_Year_for_Given_Quarter_and_Month_and_Week",
startDayOfWeek:"Friday",returnDayOfWeek:"Tuesday", QuarterOfYear:3, MonthOfQuarter:2, WeekOfMonth:3}) // Output : 18th Aug 2015
If first day of week is Tuesday, what week number in year will be
follow in 15th June 2015 as one of the date.
DateLibrary.getWeekNumber(new Date("2015-06-15"),
{operationType:"Week_of_Year",
startDayOfWeek:"Tuesday"}) // Output : 24
There are Date Difference functions also available
DateLibrary.getDateDifference(new Date("2016-04-01"),new Date("2016-04-16"),
{granularityType: "days"}) //output 15
Function for Convert number to Timestr
DateLibrary.getNumberToTimeStr("345", {delimiter: ":"}) //output 00:03:45
It also supports Julian date conversion
DateLibrary.julianToDate("102536") //output Fri Jun 20 2003 00:00:00
There is a JavaScript implementation of RFC 2445 recurrence rules : http://code.google.com/p/google-caja/source/browse/trunk/src/com/google/caja/demos/calendar/rrule-cajita.js which requires some files in the same directory. See the unit test ( http://code.google.com/p/google-caja/source/browse/trunk/tests/com/google/caja/demos/calendar/rrule_test.js ) for examples of how it works.
Try using it to parse RRULE:FREQ=WEEKLY;BYDAY=TU;INTERVAL=2 which means every second (because of the interval) week (because of the frequency) on Tuesday (because of the byday).
Have a look at the date.js library. It has several date parsing helpers including Date.today().next().tuesday() (among others).

Categories

Resources