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

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();

Related

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

<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.

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;
}
})();

Google Apps Script to remove hangouts links from calendar events

I have successfully accessed a Google calendar event and can change the color, but I'm stuck when it comes to removing the hangouts link.
I'm attempting to use code to automatically remove the hangouts links when I am the meeting originator and have not changed the hangout name, but I'm not having any success in actually changing the link.
Any help greatly appreciated.
function removehangout() {
var calendarId = 'primary';
//var calendars = CalendarApp.getOwnedCalendarsByName('Mirage ELDRIDGE');
var now = new Date();
// Determines how many events are happening in the next 24 hours x 1 days.
var now = new Date();
var endr = new Date(now.getTime() + (1 * 24 * 60 * 60 * 1000));
var events2 = CalendarApp.getDefaultCalendar().getEvents(now, endr);
Logger.log('Number of events: ' + events2.length);
var events = Calendar.Events.list(calendarId, {
timeMin: now.toISOString(),
singleEvents: true,
orderBy: 'startTime',
maxResults: 5
});
if (events.items && events.items.length > 0) {
for (var i = 0; i < events.items.length; i++) {
var event1 = events.items[i];
var d = event1.description;
var ttitle = event1.summary;
var whoby = event1.creator.email;
Logger.log(ttitle + ' by: ' + whoby);
if(whoby.indexOf('mirage.eldridge') != -1){
Logger.log(ttitle + '--> was by mirage');
var hangoutname = event1.hangoutLink;
Logger.log(ttitle + '--> hangout link name --> ' + hangoutname);
if (hangoutname != null) {
if (hangoutname.indexOf('mirage-eldridge') != -1){
//delete this link here
Logger.log(ttitle + '--> remove hangout');
//event.setHangoutLink(null);
//var idno = event.iCalUID
//CalendarApp.getEventSeriesById(idno)
event1.HangoutLink = null;
Logger.log(ttitle + '... ' + event1.hangoutLink);
Logger.log(event1.iCalUID);
//event.setcolorId('11');
var event2 = Calendar.Events.get(calendarId, event1.id)
event2.colorId = 9;
event2.hangoutLink = 'fred';
//Calendar.Events.patch(event2,calendarId,event1.id);
Calendar.Events.update(event2,calendarId,event1.id);
}
} else {
Logger.log(ttitle + ' -- do not remove ' + hangoutname);
}
}
if (!d)
d = '';
//var foundlinkyes = d.indexOf('HangoutLink');
//var actuallink = event.hangoutLink;
//var hasrlink = 'True';
//if (!actuallink) {
//Logger.log(ttitle + ' no link found');
//hasrlink = "False";
//}
//Logger.log('desc: ' + ttitle + '-- foundyes: ' + foundlinkyes);
//if (foundlinkyes == -1 && hasrlink == 'True'){
//if (event.hangoutLink && (d.indexOf('Hangout: ')== -1)){
//Logger.log (event.summary + ' - ' + event.hangoutLink + ' - ' + event.description);
//Logger.log(ttitle + ' added this ' + event.hangoutLink);
//event.description = 'HangoutLink: ' + event.hangoutLink + '\n\n' + d;
//Calendar.Events.update(event, calendarId, event.id);
//foundlinkyes = 0;
//hasrlink = 'True';
//}
//}
}
} else {
Logger.log('No events found.');
}
}

Having trouble adding msecounds to this as3 code

Hi everyone I have a code snippet I am tring to add to my flash game. It works fine I am just having trouble getting it to display milisecounds. Any help would be great!
function formatTime( time:Number ):String {
var remainder:Number;
var hours:Number = time / ( 60 * 60 );
remainder = hours - (Math.floor ( hours ));
hours = Math.floor(hours);
var minutes = remainder * 60;
remainder = minutes - (Math.floor ( minutes ));
minutes = Math.floor(minutes);
var seconds = remainder * 60;
remainder = seconds - (Math.floor ( seconds ));
seconds = Math.floor(seconds);
var hString:String = hours < 10 ? "0" + hours:"" + hours;
var mString:String = minutes < 10 ? "0" + minutes:"" + minutes;
var sString:String = seconds < 10 ? "0" + seconds:"" + seconds;
if (time < 0 || isNaN(time)){
return "00:00";
}
if (hours > 0) {
return hString + ":" + mString + ":" + sString;
}
else{
return mString + ":" + sString;
}
}
To get milliseconds, you want to take fractional part of time, it's done like that:
var mseconds:Number=Math.floor((time-Math.floor(time))*1000);
var msString:String=(mseconds<10) ? "00"+mseconds :
(mseconds<100) ? "0"+mseconds : ""+mseconds;
Then you add that to your formed string.