I'm working with a number of dates in Google Sheets, but it seems to always enter the date as a string.
sheet.getRange(3,11,sheet.getLastRow()-3, 1).setValue(Utilities.formatDate(new Date(), "UTC-5", "dd/MM/yyyy"));
is how I enter the date, but when I test it with
Logger.log(typeof extractedDate); it tells me that its a string.
If I'm working with hundreds of dates, it seems to me that it would be a huge waste of time/resources to have to parse each date as a string each time and convert it.
Instead of
sheet.getRange(3,11,sheet.getLastRow()-3, 1).setValue(Utilities.formatDate(new Date(), "UTC-5", "dd/MM/yyyy"));
use
sheet.getRange(3,11,sheet.getLastRow()-3, 1).setValue(new Date());
The above will pase the current date and time to the spreadsheet which will be displayed accordingly to the cell formatting.
If you want to pass the date without the time, instad of new Date() use
new Date(new Date().getFullYear(),new Date().getMonth() , new Date().getDate())
Better alternative
var now = new Date();
var today = now.setHours(0,0,0,0);
sheet.getRange(3,11,sheet.getLastRow()-3, 1).setValue(today);
Related
Set Date/Time to 00:00:00
How to get current date without time?
Javascript date method without time
Script inserted today's date into spreadsheet is wrong
create button to add new line with today date and blank other fields
Related
I am very new to JavaScript and had a question. I have done research and I just can't figure out why the following function isn't working for me. I am trying to establish Current Date and then getting various values from that value such as Current Date minus one year, Current Date plus one year, etc.
For Current Date minus year I have the following but it's not working (I would much prefer to have a one liner for other reasons:
var currentDateMinusOneYear = new Date(new Date().setFullYear(new Date().getFullYear() - 1));
console.log(currentDateMinusOneYear);
First we should get fullYear from a new Date, decrease by 1, set that as the year of a new Date. Then we wrap the whole thing in a Date constructor.
Console just returns an empty object. Not sure why. Please advise.
I need to query in my mongoDB in between yesterday beginning of the day and yesterday end of the day which is
T00:00:00 and T11:59:59
However everytime I add .toDate() method to any of the moment/or regular javascript methods it adds 7 hours to the time or something?
and every time i add UTC hours it adds another day which is not correct either.
What can i do?
Thanks
First you need to get yesterday date and as said in this post you can use:
let date = new Date();
date.setDate(date.getDate() - 1);
Then you can set the hour manually with .setHours() on date object, below example set the hour to midnight for instance:
let date = new Date();
date.setHours(0,0,0,0);
For more information on date Object in javascript you can look the mdn docs
If you combine both you'll have what you're looking for :)
I'm trying to get the hour in different ways from a datetime-local input but every time I've got the output as "invalid" date.
So I would like to know how can I get the hour from a datetime-local input using jquery or js.
Here is what I've tried:
var fine = moment($('#datafine').val).format("HH");
And without moment that was something like
var datafine = new Date($('#datafine').val);
When you get the value from the input, it is converted to a standard format, regardless of the displayed format:
One thing to note is that the displayed date and time formats differ from the actual value; the displayed date and time are formatted according to the user's locale as reported by their operating system, whereas the date/time value is always formatted yyyy-MM-ddThh:mm.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime-local
So you can use this with new Date(val).getHours() to get the hours.
Using jQuery:
var hour = new Date($('input[type="datetime-local"]').val()).getHours()
console.log($('input[type="datetime-local"]').val(), hour)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label for="party">Enter a date and time for your party booking:</label>
<input id="party" type="datetime-local" name="partydate" value="2019-06-01T19:30">
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getHours
If I understand correctly what you're trying to do, something like this may help. You would just need to add in the date value from your input into new Date(). The code below would give you the hours in the specified locale.
const date = new Date();
const options = { hour12: false, hour: 'numeric'};
const hours = date.toLocaleTimeString('en-US', options);
console.log(hours)
Date.prototype.toLocaleTimeString() on MDN
I am working with a sun calculator and need a reference date/time that is NOT affected of timezones or DST changes. How much I am trying, javascript (and the browser, I presume), destroy the smooth run of the sun in the sky. How can I create a date/time that I can step through in minute, hour, day, month steps without this damn DST interfering it? - Zones and localizing is no problem.
The promblem seems to be, that all of my defined UTC dates always do jump into and out of the DST. Only the real sun doesn't.
Some of my unhappy tries here:
{ //UTC DATE OVERALL
myutcdate=new Date(mydate.getUTCFullYear(),mydate.getUTCMonth(),mydate.getUTCDate(),mydate.getUTCHours(),mydate.getUTCMinutes());
//myutcdate=new Date(mydate.valueOf()-localoffset*60*60*1000);
//var utcoffset=myutcdate.getTimezoneOffset/-60;
//if(mydate.isDST() && !myutcdate.isDST())myutcdate.setHours(myutcdate.getHours()+1);
//if(myutcdate.isDST())myutcdate.setHours(myutcdate.getHours()-1);
//myutcdate= new Date(mydate.getUTCFullYear(),mydate.getUTCMonth(),mydate.getUTCDate(),mydate.getUTCHours(),mydate.getUTCMinutes(),);
//myutcdate= new Date(mydate.toUTCString());
//myutcdate= new Date(mydate).toISOString();// returns just a sring, not a date, and cannot be converted to a date
//myutcdate= new Date(mydate.toISOString());//returns plain local time
//myutc= new Date(mydate.valueOf() - parseInt(localoffset)*60*60*1000 );
//myutcdate=Date.UTC(myutc.getFullYear(),myutc.getMonth(),myutc.getDate(),myutc.getHours(),myutc.getMinutes());
//myutcdate= new Date(myutcdate);
//document.getElementById('utcdate').innerHTML=myutcdate;//.toSimpleIso(); //myutcdate with own format
//document.getElementById('utcdate').innerHTML=myutcdate.getUTCFullYear()+"-"+String(myutcdate.getUTCMonth()+1)+"-"+myutcdate.getUTCDate()+" "+myutcdate.getUTCHours()+":"+myutcdate.getUTCMinutes();//.toSimpleIso(); //Yields to reducing double the zone of source date
//document.getElementById('utcdate').innerHTML=myutcdate.getFullYear()+"-"+String(myutcdate.getMonth()+1)+"-"+myutcdate.getDate()+" "+myutcdate.getHours()+":"+myutcdate.getMinutes();//.toSimpleIso(); //myutcdate with own format
document.getElementById('utcdate').innerHTML=mydate.toISOString();
}
The Date constructor assumes local, to get UTC, use Date.UTC to generate the time value, e.g.
// Instead of
new Date(mydate.getUTCFullYear(), ...)
// Use
new Date(Date.UTC(mydate.getUTCFullYear(),...))
//-------^^^^^^^^
Then use UTC methods for everything.
var d = new Date();
// Copy date using UTC values
var c = new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), d.getUTCMilliseconds()));
console.log(d.toISOString());
console.log(c.toISOString());
I am trying to get a count on posted item from meteor-mongo matching date periods.
I inserted my posts dates as such.
posts
submitted: new Date()
the dates in the database have the following format.
yyyy-mm-dd 16:16:34.317Z // I do not understand the last part (what format it is)
I have tried this to get match the date of today from the submitted field
var currentDate = new Date();
var dd = currentDate.getDate();
var mm = currentDate.getMonth()+1;
var yyyy = currentDate.getFullYear();
var today = yyyy+'-'+mm+'-'+dd;
Posts.find({submitted: today}).count()
However, the last part is returning 0.
Is it because the last hh,mm,ss part of today is missing? If so, how can I tell meteor-mongoto ignore the time part of date so that I can return that count?
I don't like to deal with JS date objects formats, and i guess you either (I do not understand the last part (what format it is))
Give a try to momentjs package, the documentation its pretty clear.
So on the insert you can have something like.
var today = moment().format(''MMMM Do YYYY, h:mm:ss a'); // April 3rd 2015, 12:17:06 pm
Posts.insert({subbmited:today})
and do a simple find like this.
var endQuery = today..add('days', 3).calendar(); // just an example.
Posts.find({submitted: {$gt: today,$lt:endQuery}})
Here thanks to #Larry Maccherone point we are using $gte (grater than) and $lt (less than), so this works like find me post between the post submitted day and the post submitted dat + 3 days (like you ask managing ranges)
You are storing your submitted date in MongoDB with both time and timezone information. Performing a direct comparison of your currentDate with the submitted date in Mongo will never be true, since your currentDate does not contain time information.
Also you are using a String data type to query the date, which also will not work since you need to use a Date data type. Something like this will work for you:
var today = new Date();
today.setHours(0, 0, 0, 0);
Posts.find({submitted: {$gt: today}})
Which will return all the posts with a date greater than midnight of today's date.
Try something like this
posts.find({"submitted": /.*today*/})