I am trying to display the text 'Oct 09, 2012'. Instead it is not running the function and is displaying a lot of unnessecary date text. Does anyone know what I am doing wrong?
You can play with my jsfiddle... http://jsfiddle.net/UP3fd/
Here is the code...
var myDate = new Date();
convertDate(myDate);
myDate.setFullYear(2012, 9, 9);
document.write(myDate);
function convertDate(d) {
var day = d.getDate();
if (day < 10) {
day = "0" + day;
}
var year = d.getFullYear();
var month = d.getMonth();
var months=["Jan","Feb","Mar","Apr","May","June","July","Aug","Sep","Oct"," Nov","Dec"];
var currentMonth = months[month];
return (currentMonth + " " + day + ", " + year);
}
You are calling your function before you set your date, and you are not saving/outputting the return value anywhere.
var myDate = new Date();
myDate.setFullYear(2012, 9, 9);
document.write( convertDate(myDate) );
function convertDate(d) {
var day = d.getDate();
if (day < 10) {
day = "0" + day;
}
var year = d.getFullYear();
var month = d.getMonth();
var months=["Jan","Feb","Mar","Apr","May","June","July","Aug","Sep","Oct"," Nov","Dec"];
var currentMonth = months[month];
return (currentMonth + " " + day + ", " + year);
}
var strDate = addZero(d.getDate()) + "/" + addZero((d.getMonth() + 1))+
"/" +d.getFullYear();
alert("strDate :"+strDate)
return strDate;
}
function addZero(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
This is the correct code:
var myDate = new Date();
myDate.setFullYear(2012, 9, 9);
myDate = convertDate(myDate);
document.write(myDate);
[...]
Here's the corrected code, this should return you what you are expecting.
var myDate = new Date();
myDate.setFullYear(2012, 9, 9);
var newDate = convertDate(myDate);
document.write(newDate);
function convertDate(d) {
var day = d.getDate();
if (day < 10) {
day = "0" + day;
}
var year = d.getFullYear();
var month = d.getMonth();
var months=["Jan","Feb","Mar","Apr","May","June","July","Aug","Sep","Oct"," Nov","Dec"];
var currentMonth = months[month];
return (currentMonth + " " + day + ", " + year);
}
Related
This question already has answers here:
Incrementing a date in JavaScript
(19 answers)
Closed 1 year ago.
I'm trying to add 3 days to a random date but instead it seems as though I'm adding a month.
var d = new Date(2021, 9, 14);
var currentTime = d.getTime();
var daysToAdd = 3;
var secondsInADay = 86400;
var d = new Date(currentTime + daysToAdd * secondsInADay);
var year = d.getFullYear();
var month = ("0" + (d.getMonth() + 1)).slice(-2);
var day = ("0" + d.getDate()).slice(-2);
console.log('result is:' + year + '-' + month + '-' + day);
Try this :
var d = new Date(2021, 9, 14) ;
var daysToAdd = 3;
Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}
console.log(date.addDays(daysToAdd));
You're multiplying by the number of seconds in a day, but you need to multiply by the number of milliseconds in a day, as shown below:
var d = new Date(2021, 9, 14);
var currentTime = d.getTime();
var daysToAdd = 3;
var milisecondsInADay = 86400000;
var d = new Date(currentTime + daysToAdd * milisecondsInADay);
var year = d.getFullYear();
var month = ("0" + (d.getMonth() + 1)).slice(-2);
var day = ("0" + d.getDate()).slice(-2);
console.log('result is:' + year + '-' + month + '-' + day);
In google apps script I use often in summertimeperiod GMT+2 or in wintertime GMT+1 for the correct date.
To avoid to change this twice a year manually i developped following function:
function StartSummerWintertime() {
var i;
var today;
var year;
var SST;
var SWT;
var day;
var GMT_time;
today = new Date();
Logger.log("today = " + today)
year = today.getFullYear();
//Summertime starts last Sunday in March.
for (i = 31; i > 24; i--) {
SST = new Date("March " + i + "," + year);
day = SST.getDay();
if (day == 0) {
break;
}
}
Logger.log("Start date Summertime "+year+" = " + SST)
//Wintertime starts last Sunday in October.
for (i = 31; i > 24; i--) {
SWT = new Date("Oct " + i + "," + year);
day = SWT.getDay();
if (day == 0) {
break;
}
}
Logger.log("Start date Wintertime "+year+" = " + SWT)
//The switch takes place at 2:00 AM, so I have added 2 hours (= 2 * 60 * 60 * 1000 ms)
if (today.getTime() < (SST.getTime() + 2*60*60*1000) || today.getTime() > SWT.getTime() + (2*60*60*1000)) {
GMT_time = "GMT+1";
}
else {
GMT_time = "GMT+2";
}
Logger.log("GMT_time = " + GMT_time)
var date1 = Utilities.formatDate(new Date("July 6, 2021"), "GMT+2", "d-M-yyyy");
var date2 = Utilities.formatDate(new Date("July 6, 2021"), "GMT+1", "d-M-yyyy");
Logger.log("date1 = " + date1)
Logger.log("date2 = " + date2)
var date3 = Utilities.formatDate(new Date("July 6, 2021"), GMT_time, "d-M-yyyy");
var date4 = Utilities.formatDate(new Date("July 6, 2021"), GMT_time, "d-M-yyyy");
Logger.log("date3 = " + date3)
Logger.log("date4 = " + date4)
}
Date 1 and date 2 give different dates, date 3 and date 4 give correct (same) dates. The problem is solved however there should be a smarter (shorter) way to do so.
Please advise.
I have a formatted date as a string '021519' using javascript which return NAN on display in html.
Note I have a xslt using the javascript.
var newDate = '';
var formatedDate = new Date(date);
var year = formatedDate.getFullYear().toString();
var month = (1 + formatedDate.getMonth()).toString();
if(parseInt(month) < 10)
{
month = "0" + month;
}
var day = formatedDate.getDate().toString();
if(dateFormat == '1')
{
newDate = month + day + year.substr(2,4);
}
else
{
newDate = month + day + year;
}
var newLeftStart3 = parseInt(startPosition) - 1;
var newLeftEnd3 = newLeftStart + newDate.length;
var newRightStart3 = parseInt(endPosition) - newDate.length;
var newRightEnd3 = newRightStart + newDate.length;
if(alignment == '1')
{
addendaSpace = addendaSpace.substr(0, newLeftStart3) + newDate + addendaSpace.substr(newLeftEnd3);
}
if(alignment == '2')
{
addendaSpace = addendaSpace.substr(0, newRightStart3) + newDate + addendaSpace.substr(newRightEnd3);
}
newDate is displaying as NaN i hope this code helps.
I usually format dates in javascript this way:
var d = new Date();
var date = d.getDate();
var month = d.getMonth() + 1; //Months are zero based
var year = d.getFullYear();
console.log(date + "-" + month + "-" + year);
I want to get all dates in between 2 dates. So here I have mentioned statdate is date and end date is weekdate. In between 2 dates I want all dates.
Actully I am getting all dates But Not proper Format ,what i want in this format DD/MM/YY.
Now I am Getting in default Format (Sat Jun 09 2007 17:46:21)
$(document).ready(function () {
$("#day").click(function () {
startJsonSession();
return false;
});
function startJsonSession() {
var inputdate = $('#inputdate').val();
//alert("Input Date!!!" + inputdate );
var d = new Date(inputdate);
var nowMS = d.getTime(); // get # milliseconds for today
//alert(nowMS);
var week = 1000 * 60 * 60 * 24 * 7; // milliseconds in one week
//alert(week);
var oneWeekFromNow = new Date(nowMS + week);
//alert("oneWeekFromNow!!!" + oneWeekFromNow);
var fromdate = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();
if (fromdate < 10) {
fromdate = "0" + fromdate;
}
if (month < 10) {
month = "0" + month;
}
//var date = fromdate + "/" + month + "/" + year;
var date = year + "/" + month + "/" + fromdate;
alert("InputDate!!!!" + date);
//var weekdate=oneWeekFromNow.getDate() + "/" + month + "/" + year;
var weekdate = year + "/" + month + "/" + oneWeekFromNow.getDate();
alert("weekdate!!!" + weekdate);
var tomorrow = new Date(d.getTime() + (24 * 60 * 60 * 1000));
var tomorrowdate = tomorrow.getDate();
var month1 = tomorrow.getMonth() + 1;
var year1 = tomorrow.getFullYear();
if (tomorrowdate < 10) {
tomorrowdate = "0" + tomorrowdate;
}
if (month1 < 10) {
month1 = "0" + month1;
}
//var nextday = tomorrowdate + "/" + month1 + "/" + year1;
var nextday = year1 + "/" + month1 + "/" + tomorrowdate;
alert("tomorrow!!!!" + nextday);
var d1 = new Date(date);
alert("D1!!!!!" + d1.);
var d2 = new Date(weekdate);
var aDates = [];
do {
aDates.push(d1.toString());
d1.setDate(d1.getDate() + 1);
}
while (d1 <= d2);
alert("Dates!!!" + aDates);
//alert(aDates.join("\n"));
}
});
You can do it in this way
$("#getDate").click(function () {
var start = $("#startdate").datepicker("getDate"),
end = $("#enddate").datepicker("getDate");
currentDate = new Date(start),
between = [];
while (currentDate < end) {
between.push(new Date(currentDate));
currentDate.setDate(currentDate.getDate() + 1);
}
for (var i = 0; i < between.length; i++) {
var date = $.datepicker.formatDate('dd/mm/yy', new Date(between[i]));
between[i] = date;
}
console.log(between)
})
Here 'between' is the array which contains all your required Date
SEE DEMO HERE
alert("Dates!!!" + aDates.getDate()+"/"+ (aDates.getMonth()+1)+"/"+ aDates.getFullYear());
You seem to want to get a array of date strings in d/m/y format given an input string in the same format. The following functions will do that.
// Parse a string in dmy format
// return a date object, NaN or undefined
function parseDMY(s) {
var b = s.match(/\d+/g);
if (b) {
return new Date(b[2], --b[1], b[0]);
}
}
// Given a date object, return a string in dd/mm/yyyy format
function formatDMY(date) {
function z(n){return (n<10? '0' : '') + n;}
return z(date.getDate()) + '/' + z(date.getMonth() + 1) + '/' + date.getFullYear();
}
function getWeekDates(s) {
var d = parseDMY(s);
var dates = [];
if (d) {
for (var i=0; i<7; i++) {
dates.push(formatDMY(d));
d.setDate(d.getDate() + 1);
}
return dates;
}
}
console.log(getWeekDates('7/7/2014').join());
// 07/07/2014,08/07/2014,09/07/2014,10/07/2014,11/07/2014,12/07/2014,13/07/2014
Note that adding 1 day to a date is preferred over adding milliseconds as it allows the Date object to take account of daylight saving changes that might be involved.
Like i need to get todays date in format like 20120924 (yyyymmdd).How can i get this in javascript.
You could add a method to the date prototype, so you can use it on any date object:
Date.prototype.toMyString = function () {
function padZero(obj) {
obj = obj + '';
if (obj.length == 1)
obj = "0" + obj
return obj;
}
var output = "";
output += this.getFullYear();
output += padZero(this.getMonth()+1);
output += padZero(this.getDate());
return output;
}
var d = new Date();
alert(d.toMyString()); // Today
var otherDate = new Date(2012,0,1);
alert(otherDate.toMyString()); //Jan 1 2012
Fiddle: http://jsfiddle.net/johnkoer/4rk7K/10/
This worked for me.
var rDate = (new Date()).toISOString().slice(0, 10).replace(/-/g, "");
Try this.
var date = new Date();
var year = date.getFullYear().toString();
var month = date.getMonth().toString();
var day = date.getDate().toString();
if (parseInt(month) < 10) month = "0" + month;
if (parseInt(day) < 10) day = "0" + day;
var parsedDate = year + month + day;
(edit)
Improved this function by making the day equate to the day of the month, rather than the day of the week.
How about
date = new Date().toJSON().substr(0,10).split("-")
date = date[0] + date[1] + date[2]
Edit:
This will return the UTC date, not local date...
For local date, you could use:
date = new Date().toLocaleDateString().split("/"); // "M/D/YYYY"
date[0] = date[0].length == 1 ? "0" + date[0] : date[0];
date[1] = date[1].length == 1 ? "0" + date[1] : date[1];
date = date[2] + date[0] + date[1];
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
document.write(curr_year + curr_month + curr_date);
That should give the right date:)