This question already has answers here:
How to subtract 2 hours from user's local time?
(2 answers)
Closed 3 years ago.
I'm trying to substract a timeStamp in node js (google cloud functions).
This is my timestamp:
var currentTime = admin.database.ServerValue.TIMESTAMP.toString();
console.log(currentTime); //1537806936331
I would like to get the currentTime - 8 hours.
How can I do that?
(i'm using it with in a cloud function (firebase))
Thanks in advance for the help & effort!
This timestamp 1537806936331 is equal to Monday, September 24, 2018 7:35:36.331 PM GMT+03:00 DST. You can find here:
https://www.epochconverter.com/
Therefore try the following:
let date = new Date(1537806936331); //Mon Sep 24 2018 19:35:36 GMT+0300 (Eastern European Summer Time)
let hours = date.getHours() - 8; //11
Check here for more info:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
If you want to get the timestamp of the date above then try the following:
let date = new Date(1537806936331); //Mon Sep 24 2018 19:35:36 GMT+0300 (Eastern European Summer Time) //11
date.setHours(date.getHours() - 8); //11
let timeStamp = Date.parse(date);
console.log(timeStamp); //1537778136000
You can use Date.parse() to get the timestamp.
The timestamp you have is the number of milliseconds that have passed since the epoch. This means you can also use a regular subtraction of millisecond to get the timestamp of 8 hours before.
So a simpler version to Peter's (correct) answer is:
var currentTime = admin.database.ServerValue.TIMESTAMP;
var eightHoursAgo = currentTime - 8*60*1000;
Where 8*60*1000 is the number of milliseconds in 8 hours.
Related
I would like to restrict some actions if date is on sunday - e.g. do not allow create item on Sunday.
I use:
public isSunday(date: Date): boolean {
return date.getDay() === 0;
}
My problem is that my UTC offset is UTC+2.
Server and database runs on UTC TZ to prevent unwanted date and time shifts.
When I send datetime from frontend I use date.toISOString(), so my local datetime
2022-05-16 00:00:00 is converted to string 2022-05-15T22:00:00:000Z
When I check this date on the backend side, this date IS sunday, but in the UTC zone, not my local zone.
String value is converted to Date at the backend using following
new Date(input);
Edit: Value 2022-05-16T01:41:00+02:00 (sending value with utc offset info) does not work either
To my understanding, you need the local (UTC+2) zone on the back-end for only Sunday checks.
You can simply subtract 2 hours equal to milliseconds from the date received on the backend before the Sunday checks.
// ... get date here
date -= (2 * 60 * 60 * 1000); // subtract 2 hours
I don't think this is a good practice as your frontend may change its timezone but you can try this.
const date = new Date("2022-05-16 00:00:00")
const corrected_date = new Date(date.valueOf() - 7200000) //subtract milliseconds as needed, in this case 2*60*60*1000
console.log(corrected_date.getDay())
If possible, then you should maybe try sending the UTC offset as a seperate parameter to the backend and calculate the correct value to be subtracted for it to work dynamically.
Why is the result for the periodBetween.Days and substracted.Days different?
I can see that a periodBetween.Months is 0 and substracted.Months is 2 and I can see how are these two results different, but I don't know why :).
using NodaTime;
void Main()
{
var firstDate = new LocalDate(2020, 8, 1);
var secondDate = new LocalDate(2020, 10, 30);
var periodBetween = Period.Between(firstDate, secondDate, PeriodUnits.Days);
var subtracted = secondDate - firstDate;
Console.WriteLine(periodBetween.Days);
Console.WriteLine(subtracted.Days);
}
Your periodBetween calculation is saying "What's the period between these two dates, using only days?"
Your subtracted calculation is equivalent to calling Period.Between either without specifying any units, or specifying PeriodUnits.Days | PeriodUnits.Months | PeriodUnits.Years - in other words, "What's the period between these two dates, using days, months and years?"
A period has independent values for years, months, days, hours, minutes etc - if you compute a value using years/months/days that's not equivalent to computing a value using just days.
A period of "90 days" is not the same as a period of "2 months and 29 days". In some calculations they'll give the same answer, but not always. For example, if you add "90 days" to January 1st 2020, you'll get March 31st 2020... but if you add "2 months and 29 days" to January 1st 2020, you'll get March 30th 2020.
I have a scenario, where I need to schedule a job using quartz which triggers every week from the date specified by the user and this should continue for exactly one year.
After going through Cron Schedule examples, I think below cron expression might help me:
eg. If date specified is 31-10-2015, then the expression would be:
" 0 30 20 31/7 10-10 ? 2015-2016 " which means starting from 31 Oct 3015, trigger after every 7 days for 1 year, ie. till 31 Oct 2016.
Please let me know if there are any issues with this expression.
Thanks.....
Your cron-expression is not valid as per CronMaker.Com. Looking at your cron expression, I have following things:
Your job fires at 8:30PM
It is weekly job running all 7 days of week
It ends in 2016
Here is the correct cron expression :
0 30 20 ? * 2,3,4,5,6,7,1 2015,2016 //Runs all days of week
To expire it on 31st Oct in 2016, you will have to provide Ending time while creating this job.
Update:
If you want to fire this job on a particular day of week, it will be something as below:
0 30 20 ? * MON 2015,2016 //Runs every week on monday
even i had same requirement, i implemented as below
we can run for every seven days from the start date, seven days when converted to hours, value is 168
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("json", json);
String startDateStr = "2017-06-21 00:00:00.0";
Date startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(startDateStr);
String endDateStr = "2018-06-21 00:00:00.0";
Date endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(endDateStr);
JobDetail job = newJob(SimpleJob.class).withIdentity(name, "group1").build();
Trigger trigger = TriggerBuilder.newTrigger()
.startAt(startDate)
.withSchedule(
SimpleScheduleBuilder.simpleSchedule().withIntervalInHours(168).repeatForever())
.endAt(endDate)
.usingJobData(jobDataMap)
.build();
sched.scheduleJob(job, trigger);
sched.start();
hope it helps!!
In Node, I am using Moment for time and on the front-end I am using the D3 package. For moment, I will get a time of 1429588800 which is converts to "Tue Apr 21 2015 00:00:00 GMT-0400". But in D3, I get "05/031976". This is with the format "%x".
The problem I believe is that you use seconds (unix timestamp). If you multiply it by 1000 it should work. Here are some tests for you
var value = 1429588800;
var f = d3.time.format("%x");
var date = new Date(value*1000);
var moment_date = moment.unix(value);
console.log('date',date); //date Tue Apr 21 2015 00:00:00 GMT-0400 (Eastern Daylight Time)
console.log('moment date',moment_date.format()); //moment date 2015-04-21T00:00:00-04:00
console.log('d3 + moment date',f(moment_date.toDate())); //d3 + moment date 04/21/15
console.log('d3 + date',f(date)); //d3 + date 04/21/15
I have a date stored in YYYY-MM-DD. This date is used to create a moment called then. I want to count days between this date and now (moment).
When counting days I use: now.startOf('day').from(then.startOf('day')).
As an example, when testing using two dates with 3 days apart, it will be 3 days until 12 AM (12 in 24-hours format).
Do you guys know any idea why?
Just wanted to update this.
The beware the difference between HH and hh.
HH is 24 hours so the start of day is 12 where as HH the start is 0.
.format('HH:mm:ss') returns 00:00:00
.format('hh:mm:ss') returns 12:00:00
See (moment docs)[http://momentjs.com/docs/#/displaying/difference/].
Your example:
var moment = require('moment');
var a = moment("2014-05-02");
var b = moment();
var whole = a.diff(b, 'days');
var fract = a.diff(b, 'days', true);
console.log("Difference is " + whole + ", or more precisely, " + fract );
Returns:
Difference is 1, or more precisely, 1.404142025462963