how to change html input type date placeholder value as a curruent Date i m using angular - html

<input type="date" name ="Date"
class="form-control" [formControl]="dateCtrl">
getDateCtr() {
var today = new Date(this.dateCtrl.value);
var dd = String(today.getDate() ).padStart(2, '0');
var mm = String(today.getMonth()+1).padStart(2, '0');
var yyyy = today.getFullYear();
this.date = dd + '-' + mm + '-' + yyyy;
this.loadAll();
}

I used the information from Mozilla regarding date, and all that you are missing in your HTML is the value option:
<input type="date" name ="Date" class="form-control" value="{{getDateCtr()}} [formControl]="dateCtrl">
I do not know what the this.loadAll() is meant to accomplish. But the JS I worked with looked like this:
getDateCtr() :string {
var today = new Date();
var d = today.getDate().toFixed().padStart(2, '0');
var m = (today.getMonth() + 1).toFixed().padStart(2, '0');
var y = today.getFullYear().toFixed();
var sday = y + "-" + m + "-" + d
return sday;
}
Note that the return type is :string for the function.

Related

Script calls newDate and adds 5 days. I am looking to format the output to just date

Script for current date + 5 sends an email with exact 5 days from now. I want this to only output the date leaving out the timestamp
current output is: Sun Jul 10 2022 09:29:16 GMT-0400 (Eastern
Daylight Time)
expected output is: 07/0/2022
Here is what I have tried.
function findEmailAddress() {
var ss = SpreadsheetApp.getActive();
var sh1 = ss.getSheetByName('Working');
var vs1 = sh1.getRange('H1:H' + sh1.getLastRow()).getValues().flat();
var sh2 = ss.getSheetByName('Match');
var vs2 = sh2.getRange('A2:B' + sh2.getLastRow()).getValues();
var matchRows = vs2.filter(row => row[0].length && vs1.includes(row[0]));
matchRows.forEach(row => {
var mailMatch = row[1];
var curDate = new Date();
var date5 = curDate.setDate(curDate.getDate() + 5);
var dateFormat = moment(date5).format('DD/MM/YY');
var sub = "Action Required!!! 5 Days Til Expiration"
var bod = 'You have a new expiration date for completion'
+ dateFormat + ' all tasks associated with this must be complete. *This email is automatically generated do not reply*'
GmailApp.sendEmail(mailMatch, sub, bod);
});
}
I am stumped as to why this doesn't work.
Try it this way:
function findEmailAddress() {
var ss = SpreadsheetApp.getActive();
var sh1 = ss.getSheetByName('Working');
var vs1 = sh1.getRange('H1:H' + sh1.getLastRow()).getValues().flat();
var sh2 = ss.getSheetByName('Match');
var vs2 = sh2.getRange('A2:B' + sh2.getLastRow()).getValues();
var matchRows = vs2.filter(row => row[0].length && vs1.includes(row[0]));
matchRows.forEach(row => {
var mailMatch = row[1];
var curDate = new Date();
curDate.setDate(curDate.getDate() + 5);
let d = Utilities.formatDate(curDate,Session.getScriptTimeZone(),"MM/dd/yyyy")
var sub = "Action Required!!! 5 Days Til Expiration"
var bod = 'You have a new expiration date for completion'
+ d + ' all tasks associated with this must be complete. *This email is automatically generated do not reply*'
GmailApp.sendEmail(mailMatch, sub, bod);
});
}

Google Sheets getDay() Shows an Incorrect Value

Date() shows the correct date while getDay() shows an incorrect value.
function Test()
{
const date = new Date();
const dateToday = Utilities.formatDate(new Date(), "GMT+8", "MM/dd/YYYY");
const dateYesterday = Utilities.formatDate(new Date(new Date().setDate(date.getDate() - 1)), "GMT+8", "MM/dd/YYYY");
var day = date.getDay();
var a = SpreadsheetApp.getActiveSheet().getRange(3, 2);
switch (day)
{
case 1:
day = "Sunday";
break;
case 2:
day = "Monday";
break;
case 3:
day = "Tuesday";
break;
case 4:
day = "Wednesday";
break;
case 5:
day = "Thursday";
break;
case 6:
day = "Friday";
break;
case 7:
day = "Saturday";
}
a.setValue("Today is " + day + " " + dateToday + ". Yesterday was " + dateYesterday);
}
When this code was executed in Google Sheet, it will output below:
Today is Tuesday 04/21/2022. Yesterday was 04/20/2022
The dates are correct while the day shows Tuesday which is incorrect. It's Thursday right now.
As another approach, in your situation, how about the following modification?
Modified script:
function sample() {
const date = new Date();
const dateToday = Utilities.formatDate(new Date(), "GMT+8", "MM/dd/YYYY");
const dateYesterday = Utilities.formatDate(new Date(new Date().setDate(date.getDate() - 1)), "GMT+8", "MM/dd/YYYY");
const day = Utilities.formatDate(new Date(), "GMT+8", "EEEE"); // Added
const a = SpreadsheetApp.getActiveSheet().getRange(3, 2);
a.setValue("Today is " + day + " " + dateToday + ". Yesterday was " + dateYesterday);
}
In this modification, the day name is retrieved using Utilities.formatDate like Utilities.formatDate(new Date(), "GMT+8", "EEEE").
Reference:
formatDate(date, timeZone, format)
The getDay() method returns the day of the week for the specified date according to local time, where 0 represents Sunday.
Sunday is 0, Monday is 1, etc.
docs
And example with a less length implementation:
const getDayofWeek = x => x === 0 ? 'Sunday' : x === 1 ? 'Monday' : x === 2 ? 'Tuesday' : x === 3 ? 'Wednesday' : x === 4 ? 'Thursday' : x === 5 ? 'Friday' : x === 6 ? 'Saturday' : "Invalid Day";
for (i = 0; i < 8; i++) {
console.log(getDayofWeek(i))
}
Try
function test() {
const dayOfTheWeek = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
const date = new Date();
console.log(dayOfTheWeek[date.getDay()])
}
const dayOfTheWeek = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
const date = new Date();
console.log(dayOfTheWeek[date.getDay()])
The most important thing is to check your timezone in your script editor : go to your script editor, ckick on the gear on the left hand side, check the third box, go back to script editor and review in appsscript.json, hange timezone according to your locale
Try it this way:
function Test() {
const dt = new Date();
const dateToday = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "MM/dd/yyyy");
const dateYesterday = Utilities.formatDate(new Date(dt.getFullYear(), dt.getMonth(), dt.getDate() - 1), Session.getScriptTimeZone(), "MM/dd/yyyy");
var d = dt.getDay();
var a = SpreadsheetApp.getActiveSheet().getRange(3, 2);
var day;
switch (d) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
}
Logger.log("Today is " + day + " " + dateToday + ". Yesterday was " + dateYesterday)
a.setValue("Today is " + day + " " + dateToday + ". Yesterday was " + dateYesterday);
}

Nuxtjs/Vuejs set the default date time as current date time for the input field of type datetime-local

I have a Nuxtjs/Vuejs application that contains the input field of type datetime-local. For this field, I would like to add the current DateTime as the default value. I have done similar things in AngularJS but for some reason, it is not working in Vuejs.
Following is the field:
<input v-model="formData.eventtimeSpecific" type="datetime-local" class="form-control" title="Set Specific Event Time">
Following is the JS function for it:
export default {
data(){
return {
formData:{
eventtimeSpecific: new Date(),
}
}
},
mounted () {
const today = new Date()
this.formData.eventtimeSpecific.setHours(today.getHours(), today.getMinutes(), 0, 0)
},
}
Similar approach when I tried in Angularjs it was working:
$scope.formdata.eventtimeSpecific = new Date();
var h = $scope.formdata.eventtimeSpecific.getHours();
var m = $scope.formdata.eventtimeSpecific.getMinutes();
$scope.formdata.eventtimeSpecific.setHours(h,m,0,0);
Can someone please assist me in how to set the current DateTime value as the default value for datetime-local type input field?
Current behavior:
Expected behavior:
The problem is that the
this.formData.eventtimeSpecific.setHours(today.getHours(), today.getMinutes(), 0, 0) returns time in this format Wed Aug 25 2021 13:35:49 GMT+0200 but, type="datetime-local" accepts only this format 2021-08-25T13:36
So you have to format it:
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1;
var day = now.getDate();
var hour = now.getHours();
var minute = now.getMinutes();
var localDatetime =
year +
'-' +
(month < 10 ? '0' + month.toString() : month) +
'-' +
(day < 10 ? '0' + day.toString() : day) +
'T' +
(hour < 10 ? '0' + hour.toString() : hour) +
':' +
(minute < 10 ? '0' + minute.toString() : minute);
this.formData.eventtimeSpecific = localDatetime;
Hope It helped!
EDIT!
Here is easier way to do it
this.formData.eventtimeSpecific.setMinutes(
this.formData.eventtimeSpecific.getMinutes() - this.formData.eventtimeSpecific.getTimezoneOffset()
);
this.formData.eventtimeSpecific = this.formData.eventtimeSpecific.toISOString().slice(0, -8);
The slice -8 is there for not including the seconds.

Date Bookmarklet not working in chrome

Below is a bookmarklet that is not working can someone help me out.
javascript:function url() {
var date = new Date();
date.setDate(date.getDate() + (1 + 7 - date.getDay()) % 7);
var y = date.getFullYear();
var m = date.getMonth() +1;
if(m < 10){m = '0' + m;}
var d = date.getDate();
if(d < 10){d = '0' + d;}
var date = y + "-" + m + "-" + d;
return 'https://wms.blueapron.com/facilities/4/grocery-board?view_type=cumulative&week_starts_on=' + date
}window.open(url(),"_blank");
The working bookmarklet may look like this:
javascript:(function() {
window.open(url(),"_blank");
function url() {
var date = new Date();
date.setDate(date.getDate() + ((8 - date.getDay()) % 7 ? (8 - date.getDay()) % 7 : 7));
var y = date.getFullYear();
var m = date.getMonth() +1;
if(m < 10){m = '0' + m;}
var d = date.getDate();
if(d < 10){d = '0' + d;}
var date = y + "-" + m + "-" + d;
return 'https://wms.blueapron.com/facilities/4/grocery-board?view_type=cumulative&week_starts_on=' + date;
}
})();

HTML5 Input datetime-local default value of today and current time

Is there anyway that I can make a default value of HTML5 input type='datetime-local' to today's date and this current time.
Thanks before
You can make it shorter:
<input type="datetime-local" id="cal">
window.addEventListener('load', () => {
var now = new Date();
now.setMinutes(now.getMinutes() - now.getTimezoneOffset());
/* remove second/millisecond if needed - credit ref. https://stackoverflow.com/questions/24468518/html5-input-datetime-local-default-value-of-today-and-current-time#comment112871765_60884408 */
now.setMilliseconds(null)
now.setSeconds(null)
document.getElementById('cal').value = now.toISOString().slice(0, -1);
});
It's possible. By using a JQuery function, you can have a really complete solution.
Here is an example.
JSFiddle http://jsfiddle.net/v8MNx/1/
HTML
<form action="demo.html" id="myForm">
<p>
<label>Date:</label>
<input type="datetime" name="anniversaire" id="anniversaire"/>
</p>
<input type="submit" value="Submit"/>
</form>
JQuery:
//Function found here: https://gist.github.com/ryanburnette/8803238
$.fn.setNow = function (onlyBlank) {
var now = new Date($.now())
, year
, month
, date
, hours
, minutes
, seconds
, formattedDateTime
;
year = now.getFullYear();
month = now.getMonth().toString().length === 1 ? '0' + (now.getMonth() + 1).toString() : now.getMonth() + 1;
date = now.getDate().toString().length === 1 ? '0' + (now.getDate()).toString() : now.getDate();
hours = now.getHours().toString().length === 1 ? '0' + now.getHours().toString() : now.getHours();
minutes = now.getMinutes().toString().length === 1 ? '0' + now.getMinutes().toString() : now.getMinutes();
seconds = now.getSeconds().toString().length === 1 ? '0' + now.getSeconds().toString() : now.getSeconds();
formattedDateTime = year + '-' + month + '-' + date + 'T' + hours + ':' + minutes + ':' + seconds;
if ( onlyBlank === true && $(this).val() ) {
return this;
}
$(this).val(formattedDateTime);
return this;
}
$(function () {
// Handler for .ready() called.
$('input[type="datetime"]').setNow();
});
The accepted answer seems pretty complicated to me... here a shorter solution that doesn't need jQuery
JSFiddle: https://jsfiddle.net/rzaceg8v/
window.addEventListener("load", function() {
var now = new Date();
var utcString = now.toISOString().substring(0,19);
var year = now.getFullYear();
var month = now.getMonth() + 1;
var day = now.getDate();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
var localDatetime = year + "-" +
(month < 10 ? "0" + month.toString() : month) + "-" +
(day < 10 ? "0" + day.toString() : day) + "T" +
(hour < 10 ? "0" + hour.toString() : hour) + ":" +
(minute < 10 ? "0" + minute.toString() : minute) +
utcString.substring(16,19);
var datetimeField = document.getElementById("myDatetimeField");
datetimeField.value = localDatetime;
});
<input type="datetime-local" id="myDatetimeField"/>
The methods above worked but were too verbose for me.
Here's my version:
window.addEventListener("load", function() {
var now = new Date();
var offset = now.getTimezoneOffset() * 60000;
var adjustedDate = new Date(now.getTime() - offset);
var formattedDate = adjustedDate.toISOString().substring(0,16); // For minute precision
var datetimeField = document.getElementById("myDatetimeField");
datetimeField.value = formattedDate;
});
This worked perfectly!
One note, I tried this the only month it would break, October. It would give me a '010', instead of 10.
month = (now.getMonth() +1 ).toString().length === 1 ? '0' + (now.getMonth() + 1).toString() : now.getMonth() + 1;
Big line works for me, if it doesn't for you you can introduce whitespaces and line breaks in the expressions.
function setDatetimeInput(element, t = new Date()){
function p(number){return number.toString().padStart(2, '0');}//number to 2 digit, 0 padded string
element.value = `${t.getFullYear()}-${p(t.getMonth()+1)}-${p(t.getDate())}T${p(t.getHours())}:${p(t.getMinutes())}`;
}
here's a simple way:
<input type="datetime-local" id="cal">
const currentDateTime = () => {
var tzoffset = new Date().getTimezoneOffset() * 60000; //offset in milliseconds
var localISOString = new Date(Date.now() - tzoffset)
.toISOString()
.slice(0, -1);
// convert to YYYY-MM-DDTHH:MM
const datetimeInputString = localISOString.substring(
0,
((localISOString.indexOf("T") | 0) + 6) | 0
);
console.log(datetimeInputString);
return datetimeInputString;
};
document.getElementById('cal').value = currentDateTime();