I need to add getTime from giving the date to another date format yyyy-mm-dd using javascript.
this is my date :
var m = new Date(res[i].DATESTART).getTime();
add m to another date.
var ddm = formatDate(monday);
var dddm = ddm+" 23:59:59";
var newdate = new Date(dddm);
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
}
Related
I'm new with Javascript and I would like to create a Google Tag Manager function to return the date of Today + 7 days and format the result in "YYYY-MM-DD",
The purpose is to use this variable in the Google Review script that requires a estimated delivery date.
Example :
"estimated_delivery_date": "YYYY-MM-DD",
I tried the following function that return "undefined"
As I'm really new to JS, I don't know how to fix this function to make it work in Google Tag Manager the way I need it to.
var sevenDaysFromNow = new Date().setDate(new Date().getDate() + 7);
function formatDate(sevenDaysFromNow) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
}
Fix a small issue in your function:
var sevenDaysFromNow = new Date().setDate(new Date().getDate() + 7);
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
}
console.log(formatDate(sevenDaysFromNow));
I found the answer some minutes after posting this.
Scenario: I serialize a date from a C# ControllerAction and store that in a js variable,
which I then convert to a js date, via toISOString, but the js date is then the previous day. I think the issue is the dateTimeOffset, but I don't know how to resolve it.
I have tried getting the local TimeZoneOffset and adding that to the js date, but without success.
The end goal is to return that same date to C# controller in the format "yyyy/mm/dd".
function getFormattedDate(inDate)
{
console.log("inDate=" + inDate); // = /Date(1564610400000)/
var d = new Date()
var tzDifference = d.getTimezoneOffset();
console.log("datetimeOffset=" + tzDifference ); // =-120
var date = new Date(parseInt(inDate.substr(6)));
// CORRECT date=Thu Aug 01 2019 00:00:00 GMT+0200 (South Africa Standard Time)
var res = date.toISOString().slice(0, 10).replace(/-/g, "");
// res=20190731 (C# has it as 2019/08/01 00:00:00) so res should be 20190801.
var yr = res.substr(0, 4);
var mth = res.substr(4, 2);
var dy = res.substr(6, 2);
var dateFormatted = yr + '/' + mth + '/' + dy;
return dateFormatted;
}
The answer:
var date = new Date(parseInt(inDate.substr(6)));
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('/');
I would like to limit the date so that the user does not have to select a date of less than one year, but I wish that it is done automatically compared to the date of the system. If anyone has ideas I'm grateful.
You can get the current date and subtract an year from the current year and set min attribute
var dtToday = new Date();
var month = dtToday.getMonth() + 1;
var day = dtToday.getDate();
var year = dtToday.getFullYear();
if(month < 10)
month = '0' + month.toString();
if(day < 10)
day = '0' + day.toString();
var minDate = (year-1) + '-' + month + '-' + day;
document.getElementById('dt').setAttribute('min', minDate);
<input type="date" id="dt">
You can specify the min and max property for the input element:
let input = document.querySelector("#test");
let nextYear = new Date(new Date().setFullYear(new Date().getFullYear() + 1));
let today = new Date();
input.max = formatDate(nextYear)
input.min = formatDate(today)
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
}
<input type="date" id="test"/>
I need DD-MM-YYYY Format of the given date.
var todaydate = /Date(1394908200000)/; //Serialize date
var date = eval("new " + todaydate.replace(/\//g, ""));
alert('date is :'+date)
but output look like,
date is :Wed Jun 11 2014 00:00:00 GMT+0530 (India Standard Time)
Expected output like,
date is :11-06-2014
Try this
var date = new Date(); //new Date(1394908200000)
function convert(str) {
var date = new Date(str),
mnth = ("0" + (date.getMonth() + 1)).slice(-2),
day = ("0" + date.getDate()).slice(-2);
return [day, mnth, date.getFullYear()].join("-");
}
var final = convert(date);
alert('date is :' + final)
DEMO
Besides of JS like other people mention, you can also use the .datepicker from jquery ui plug-in
var dt = $.datepicker.formatDate('dd-mm-yy', new Date(1394908200000));
alert(dt);
JSFiddle, use jquery ui plug-in
WORKING FIDDLE
Try this-
var date = new Date();
function myDateFormatter (dateobject) {
var d = new Date(dateobject);
var day = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();
if (day < 10) {
day = "0" + day;
}
if (month < 10) {
month = "0" + month;
}
var date = day + "-" + month + "-" + year;
return date;
};
var dateformat = myDateFormatter(date);
alert('date is :' + dateformat);
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:)