I have been testing moment js on jsfiddle and i am trying varius things such as displaying month,day and formatting dates but the month comes out wrong
This is the code link
https://jsfiddle.net/codebreaker87/z9d5Lusv/37/
and this is the code itself
var day = moment().format('dddd');
var month = moment().format('MMMM');
var date = moment().format('d');
var year = new Date().getFullYear();
var nmonth = new Date().getMonth();
var gt = new Date().getTime();
var dotdate = date+'.'+nmonth+'.'+year;
var newdate = new Date();
alert(nmonth);
alert(newdate);
alert(dotdate);
alert(gt);
var fd = moment().format('dddd, MMMM d');
var fdf = moment().format('DD-MM-YYYY');
var nd = moment(dotdate, "DD-MM-YYYY").add(5, 'days');
var frt = nd.format('dddd, MMMM d');
alert('Weekday,Month Day:: '+fd);
alert('Five Days Later:: '+nd);
alert('Five Days Later into Weekday,Month Day Format:: '+frt);
The code displays the current month as 10 instead of 11.
Why is it displaying the month this way?.
Related
I need to disable all days of all months in the calendar except for the fifth and tenth day
The user is suposse to choose only one of those two days,
example
Any Idea how to do this? rigth know I'm trying this way:
var d = new Date();
var dia = "05";
var mes = d.getMonth()+1;
mes = mes.toString();
var anio = d.getFullYear().toString();
var minDate = new Date(dia+'-'+mes+'-'+anio);
var d2 = new Date();
var dia2 = "10";
var mes2 = d2.getMonth()+1;
mes = mes2.toString();
var anio2 = d2.getFullYear().toString();
var maxDate = new Date(dia2+'-'+mes2+'-'+anio2);
var inputFechaPago = new sap.m.DatePicker({
placeholder: " ",
valueFormat: "dd/MM/yyyy",
displayFormat: "dd/MM/yyyy",
dateValue: minDate,
minDate: minDate,
maxDate: maxDate
},
});
but is not working as expected
I think you use the wrong format.
var minDate = new Date(2018, 11, 24);;
3 numbers specify year, month, and day; per example: 24. November 2018
The solution for you is:
var minDate = new Date(anio + ',' + mes + ',' + dia);
This is my code:
var date1 = '01/02/2017';
var date2 = '31/01/2020';
var startDate = new Date( date1.split("/")[2], date1.split("/")[1]-1, date1.split("/")[0] );
var endDate = new Date( date2.split("/")[2], date2.split("/")[1]-1, date2.split("/")[0] );
var diff = new Date(endDate - startDate);
var diffResult = ((diff.getFullYear() - 1970) * 12+ diff.getMonth()) + " months";
so the output is only 35 months (and also 30 days but its hidden)
but as 30 days are actually already a month, I would like to have it as 36 months.
any suggestions?
thanks
There are several good thoughts/solutions in the comments. It seems what you're really after is the number of 30-day intervals that fit between the start and end date.
After all, 30 days is not always a month as months can have 31 days.
If you're interested in how many 30-day intervals fit between a start and end date you can count the days and divide by 30:
var date1 = '01/02/2017';
var date2 = '31/01/2020';
var dateRegex = /\d+/g;
var date1Array = date1.match(dateRegex);
var date2Array = date2.match(dateRegex);
var startDate = new Date(date1Array[2], date1Array[1], date1Array[0]);
var endDate = new Date(date2Array[2], date2Array[1], date2Array[0]);
var diffResult = Math.round((endDate-startDate)/(1000*60*60*24));
var months = Math.floor(diffResult/30);
alert(months);
Credit for the equation to get the number of days goes to this question.
All the date, related Values are shown as NaN, although the same JS codes, works fine in chrome, giving me the correct values, HEre's a section, of those date codes.
var date = new Date(data.list[s].dt_txt);
var year = date.getFullYear();var month = date.getMonth()+1; var dat = date.getDate();
if(month<10){month='0'+month;}if(dat<10){dat='0'+dat;}
var hour = date.getHours();var minute = date.getMinutes(); var sec = date.getSeconds();
if(hour<10){hour='0'+hour;}if(minute<10){minute='0'+minute;}if(sec<10){sec='0'+sec}
var ftime = hour+':'+minute+':'+sec;
var fdate = year+'-'+month+'-'+dat;
///\\
var date = new Date(data.list[s+1].dt_txt);
hour = date.getHours();minute = date.getMinutes();sec = date.getSeconds();
if(hour<10){hour='0'+hour;}if(minute<10){minute='0'+minute;}if(sec<10){sec='0'+sec}
var ttime = hour+':'+minute+':'+sec;
c1.innerHTML='+fdate+';
c2.innerHTML=''+ftime+' to '+ttime+'';
c3.innerHTML=data.list[s].weather[0].main;
How do I get the Today date on google appscript?
I need to write a code to input today´s date in a cell.
function changeDate(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(GA_CONFIG);
var date = //Today´s date!?!?!!?
var endDate = date;
sheet.getRange(5, 2).setValue(endDate);
}
Utilities.formatDate(new Date(), "GMT+1", "dd/MM/yyyy")
You can change the format by doing swapping the values.
dd = day(31)
MM = Month(12) - Case sensitive
yyyy = Year(2017)
function changeDate() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(GA_CONFIG);
// You could use now Date(); on its own but it will not look nice.
var date = Utilities.formatDate(new Date(), "GMT+1", "dd/MM/yyyy")
var endDate = date
}
The Date object is used to work with dates and times.
Date objects are created with new Date()
var now = new Date();
now - Current date and time object.
function changeDate() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(GA_CONFIG);
var date = new Date();
sheet.getRange(5, 2).setValue(date);
}
Google Apps Script is JavaScript, the date object is initiated with new Date() and all JavaScript methods apply, see doc here
The following can be used to get the date:
function date_date() {
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 hour = date.getHours(); if(hour.toString().length==1){var hour = '0'+hour;}
var minu = date.getMinutes(); if(minu.toString().length==1){var minu = '0'+minu;}
var seco = date.getSeconds(); if(seco.toString().length==1){var seco = '0'+seco;}
var date = year+'·'+month+'·'+day+'·'+hour+'·'+minu+'·'+seco;
Logger.log(date);
}
Easiest way, you can use javascript date object which is new Date().
function changeDate(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(GA_CONFIG);
var date = new Date();
sheet.getRange(5, 2).setValue(date);
}
But then you will get the whole time object. You just need to change the format in spreadsheet to time or date, whatever you like.
function myFunction() {
var sheetname = "DateEntry";//Sheet where you want to put the date
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetname);
// You could use now Date(); on its own but it will not look nice.
var date = Utilities.formatDate(new Date(), "GMT+5:30", "yyyy-MM-dd");
//var endDate = date;
sheet.getRange(sheet.getLastRow() + 1,1).setValue(date); //Gets the last row which had value, and goes to the next empty row to put new values.
}
How do I get the day of the week from a timestamp in JavaScript?
I'd like to get this from a timestamp I specify, not the current date.
Thanks
var timestamp = 1400000000;
var a = new Date(timestamp*1000);
var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
var dayOfWeek = days[a.getDay()]
Now the "day of the week" is in the dayOfWeek variable.
var timestamp = 654524560; // UNIX timestamp in seconds
var xx = new Date();
xx.setTime(timestamp*1000); // javascript timestamps are in milliseconds
document.write(xx.toUTCString());
document.write(xx.getDay()); // the Day
2020 Update
If this browser support is acceptable for you you can use this one liner:
new Date(<TIMESTAMP>).toLocaleDateString('en-US', { weekday: 'long' }); // e.g. Tuesday
Similar to klidifia's answer, but for some reason the day of the week was off. I had to update the 'days' array to start on Monday.
var timestamp = 1400000000;
var a = new Date(timestamp*1000);
var days = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday', 'Sunday'];
var dayOfWeek = days[a.getDay()]
Try out the following:
var currentTime = new Date();
var day = currentTime.getDate();