- Why show an invalid date?
- How to get input on Landon's current date time? N.B: In my project moment.tz() is not working.
<script>
let time = new Date(new Date().toLocaleString('en-GB', { timeZone: 'Europe/London' }));
// let time = date.toLocaleString('en-GB', {timeZone: 'Europe/London'});
let prevRefreshTime="2023-01-15 05:20:09" //landon time
let startTime=moment(moment(prevRefreshTime).format('HH:mm:ss'), "HH:mm:ss");
let endTime=moment(moment(time).format('HH:mm:ss'), "HH:mm:ss");
let minutes =Math.abs(moment.duration(endTime.diff(startTime)).as('minutes'));
</script>
Example :
previous time:2023-01-15 06:35:56 (London timezone)
current time : current London time
minutes=previous - current time
CodePudding user response:
After working with your code sample, there are several changes I had to make to fix it.
Here is the reply link with the code fixes https://replit.com/join/opxdiovkin-drewg
Below is the code for your quick reference
const moment = require('moment-timezone');
let endTime = moment().tz('Europe/London');
let prevRefreshTime="2023-01-15 05:20:09" //london time
let startTime=moment(prevRefreshTime)
let minutes = Math.abs(endTime.diff(startTime, 'minutes'));
console.log('minutes', minutes)
- You were just using moment but the
.tz()
function can be used if you usemoment-timezone
, which has moment loaded underneath in its package. So you can just usemoment-timezone
to use the.tz()
function and moment functions Here is the link tomoment-timezone
https://momentjs.com/timezone/.
Run npm install moment-timezone
to use the package
If you are using jquery or vanilla js, try using a CDN import of the library or just downloading moment.min.js
and importing it as a script into your project. Exclude the require
statement at the beginning because it should already be imported.
Here is a Repl for a vanilla js version you can use https://replit.com/join/hraodvrlaf-drewg
You were also calling moment() functions within moment() functions which complicates the code. You don't need to format the dates until after calculation.
The
diff
function must have a second param with the unit you want to want to receive the difference result in. I added that in.
CodePudding user response:
The date provided in the example code (2023-01-15) is not an invalid date, it is a valid date in the format YYYY-MM-DD. The purpose of the date is to serve as an example of a previous refresh time, so it may not be the current date.
To get the current date and time in London, you can use the Date object and set the time zone to 'Europe/London' using the toLocaleString()
method.
let time = new Date(new Date().toLocaleString('en-GB', { timeZone: 'Europe/London' }));
Regarding moment.tz()
is not working, it is possible that moment-timezone library is not properly installed or imported in your project. Make sure it is correctly installed and imported in your project.
import moment from 'moment-timezone';
Then you can use moment.tz()
function to get the current date and time in London as following
let time = moment().tz("Europe/London").format();
Finally, you can use moment()
function to get the time difference between two time in minutes.
let startTime = moment(prevRefreshTime);
let endTime = moment(time);
let minutes = Math.abs(moment.duration(endTime.diff(startTime)).as('minutes'));
Note that, If you're comparing time in different date, you should use moment.duration(endTime.diff(startTime), 'seconds')
instead of as('minutes')