Is it possible to get the next time it's 14:00 (for example) as a Date object in Javascript using Datejs?
The documentation doesn't seem to show such an example and trying next 14:00 doesn't really seem to work.
You don't really need a library for this. You can create a Date object and if it's after 14:00, set the date to tomorrow, then set time to 14:00.
Edit
The original would allow any time from 14:00 to 14:59:59.999 as it only checked the hours. Updated so that any time from 14:00:00.001 to 23:59:59.999 is treated as after 14:00.
Also added Date.js version (the documentation for which is minimal — perhaps there's something better).
// Create a date for now and remember the time
var d = new Date();
var time = +d;
// Set the time to 14:00 and, if earlier than now, add a day
d.setHours(14,0,0,0);
if ( d < time) {
d.setDate(d.getDate() + 1);
}
document.write('Next 14:00: ' + d)
Here is the Date.js version. I couldn't find a CDN to include as a snippet.
// Equivalent using Date.js
var d = Date.present();
var t = +d;
d.set({hour:14});
if (d < t) {
d.addDays(1);
}
document.write('<br>' + d3);
Or, as one line:
Date.today().set({hour:14}).isBefore(Date.present())? Date.today().addDays(1).set({hour:14}) : Date.today().set({hour:14});
// Or
Date.today().set({hour:14}).addDays(Date.today().set({hour:14}).isBefore(Date.present())? 1:0);
I don't recommend the one liner versions; they're just there to show it can be done. They each create 3 date objects, whereas the other versions create only 1.
I do not know how to use Datejs, but here is a simple solution in pure JavaScript. Hopefully it helps a bit. This sample gets the current date and checks if it is already past 14:00, and if so, creates a Date object for 14:00 the next day. If it is not already past 14:00, it creates a Date object for 14:00 that day.
var currentDate = new Date();
if (currentDate.getHours() >= 14){
var newDate = new Date(currentDate.getYear(), currentDate.getMonth(), currentDate.getDate()+1, 14, 0, 0, 0);
}
else{
var newDate = new Date(currentDate.getYear(), currentDate.getMonth(), currentDate.getDate(), 14, 0, 0, 0);
}
Related
So there is a column that has the date with the hour and i was trying to create a variable date with the same date, month, year and hour to be able to compare it wiht that date but this didn't work with me so I thought I would do that by creating the same date but when i compare i won't consider the hour but im facing some difficulties.
anyone of the two solutions would be great
I wrote many other codes but none of them worked and that was the last one i wrote
var date = new Date();
var year = date.getYear();
var month = date.getMonth() + 1; if(month.toString().length==1){var month =
'0'+month;}
var day = date.getDate(); if(day.toString().length==1){var day = '0'+day;}
var date = month+'/'+day+'/'+year;
Logger.log(date);
Im using JavaScript in google app script.
Thank you!
From MDN
We have a first step to create an object date.
let today = new Date()
let birthday = new Date('December 17, 1995 03:24:00')
let birthday = new Date('1995-12-17T03:24:00')
let birthday = new Date(1995, 11, 17) // the month is 0-indexed
let birthday = new Date(1995, 11, 17, 3, 24, 0)
let birthday = new Date(628021800000) // passing epoch timestamp
You can create your Date object following the example above that fits you better. I also recommend giving a good look into this page.
For the second step...
From there, you can use Date.now(). As explained here, this will return "A Number representing the milliseconds elapsed since the UNIX epoch."
The third step is...
comparing both numbers. Which one is smaller will be an "earlier date" and vice-versa.
If some dates don't have time, I would consider it as midnight. Using the default Date format, that would be something like this.
yyyy-mm-ddThh:mm:ssZ
Ex:
2022-02-21T09:39:23Z
The Z at the end means UTC+0.
More about this on this link.
So, a date without time would be:
2022-02-21T00:00:00Z
I'm developing a website where registrations for a particular event will open on a certain date (say, January 1, 2019) and will close on another date (say, January 10, 2019). I'm using JavaScript to redirect users to the relevant pages if they try to access it on before the 1st or after the 10 of January.
My code so far:
var d = new Date();
var startDate = new Date(2019, 0, 1, 8);
var endDate = new Date(2019, 0, 10, 23, 59);
if(endDate-d<0) // Past expiration date
window.location = "register-closed.html";
else if(startDate-d > 0) // Before starting
window.location = "register-unavailable.html";
The main problem as you might have guessed is that this code takes the local date and time from the user; if I set the date on my device as 2nd January, 2019, I'm able to access the actual register page, even though it's May right now.
I feel this would be a common problem for many, but I've been unable to find any solution to this. How do I get the REAL date and time for my country (India) instead of the device time?
TL;DR
How do I get the actual date and time for a country (in my case, INDIA) using JavaScript? If I can't use vanilla JS, is there some other method to do so?
PS: If you have any solutions that can only be bypassed using methods more complicated than changing your device time, I'll readily accept them. This whole website is just for a high school event, so I don't expect any skilled hackers to spend their time on this :)
This code will get the date (as in the 30th), month, and year. This uses the new Date(); variable type. It has several uses, and you can get the output in whatever order using something like new Date(year, month, day, hours, minutes, seconds, milliseconds). That would output something like Wed May 22 2019 10:46:32 GMT-0400 (Eastern Daylight Time).
var todaysDate = new Date();
var date = todaysDate.getDate();
var month = todaysDate.getMonth();
var year = todaysDate.getFullYear();
if(date === 10 || month === 0 || year === 2019){
//January is 0 because counting starts at 0
...
}
https://www.w3schools.com/jsref/jsref_obj_date.asp
you should take a look to this topic as it seems to answer to your problem using only vanilla JS. Hope it helps :)
everyone!
I was looking for solution of this for a long time and didn't find anything.
I need to make element on page which will have +=2000 every month from ..let's say today's date.
And I am puzzled without any idea how to do this.
I easily wrote updating value every 10 seconds, but what about months that have different length and so on?
Should I compare difference between current date and today's date, than do +=2000*numberOfMonths? Then how often should I check if month has passed no to kill speedload?
Or is there any other convinient way to do it?
I know the solution might be easy, but I don't get it.
Will be gratefull for any suggestions.
You can do it like:
const getMonthsPassed = () => {
const startDate = new Date(2018, 10, 22); // month start at 0
const currentDate = new Date();
const monthDifference =
(currentDate.getFullYear() - startDate.getFullYear()) * 12 +
(currentDate.getMonth() - startDate.getMonth());
return monthDifference;
};
I need to create a javascript function to get yesterdays date and time as midnight 00:00:00. For example 2018-08-03 00:00:00. The set.Hours() function is not working in my case. The script is to be used in spoon pentaho data integration. How i can do this. Please help
You should firstly subtract one day, then set the hours to 00:00:00, e.g.
var d = new Date();
d.setDate(d.getDate() - 1);
d.setHours(0,0,0,0);
console.log(d.toString());
If the setHours function is not working for you (i.e. not available or broken, which seems very peculiar), then you could use:
var d = new Date();
d.setDate(d.getDate() - 1);
d = new Date(d.getFullYear(), d.getMonth(), d.getDate());
console.log(d.toString());
// Or even
var e = new Date();
e = new Date(e.getFullYear(), e.getMonth(), e.getDate() - 1);
console.log(e.toString());
In Pentaho Data Integration you should always try and use the other steps before resorting to JavaScript, as the performance lowers significantly.
Use a Formula step with the function Today()(which records the current date at 00:00:00) for a column, create a colum with the value -1, and use the Calculator step with the operation DATE A + B Days.
If at some point you want the exact time of the date, you can use the function Now() in the formula step.
I am developing a daily calendar and need to traverse to the next and previous day. How would I write this in javascript? Will the following calculate correctly when going to the next / previous month? Like C#'s DateTime.Today.AddDays(1) will?
new Date(year, month, day + 1)
My concern is that if I execute this on March 31st, it will calculate March 32nd...which wouldn't work obviously.
If someone could provide a function to do both that would be great!
Thanks in advance!
var dateString = '30 Apr 2010'; // date string
var actualDate = new Date(dateString); // convert to actual date
var newDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate()+1); // create new increased date
You don't need to worry. It won't.
date = new Date(2012, 2, 31 + 1);
console.log(date);