how to change dateformat into yyyy-mm-dd - javascript

I am using AIR DATEPICKER plugin for one of my page for datetime field.now I able to get the date format on placeholder by default 2022-09-01 . but when I select date from datetime calender it's showing me value like 22 September 2022 00:00 am . I want to get date value in 2022-09-22 00:00 am So how can I change dateformat?
Here Is my code:
<input type="text" class="form-control col-md-2 datetimepicker d-inline" id="dfield" name="scheduleField" value=""/>
Javascript code:
const dateInput = document.getElementById("dfield");
// Using the visitor's timezone
//dateInput.value = formatDate();
dateInput.value = new Date().toISOString().split("T")[0];
function padTo2Digits(num) {
return num.toString().padStart(2, "0");
}
function formatDate(date = new Date()) {
return [
date.getFullYear(),
padTo2Digits(date.getMonth() + 1),
padTo2Digits(date.getDate()),
].join("-");
// dateInput.value = new Date().toISOString().split('T')[0];
}

Try this
function formatDate() {
var d = new 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());

Related

Javascript function (tag manager) - Add 7 days from today and format it

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

Trying to get first date of current month in html date picker

Hi I am trying to get first date of current month in HTML date picker.
From <input type="date" id="fdate" value=""/>
To<input type="date" id="tdate" value=""/>
I get today date in id="tdate like this given below but starting date of current month not able to get as I get current date.
var date = new Date();
var day = date.getDate();
var month = date.getMonth() + 1;
var year = date.getFullYear();
if (month < 10) month = "0" + month;
if (day < 10) day = "0" + day;
var today = year + "-" + month + "-" + day;
document.getElementById("tdate").value = today;
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
document.getElementById("tdate").value = firstDay;
date input fields must be in YYYY-MM-DD format. Your code:
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
Will give back a string, e.g. Tue Sep 01 2020 00:00:00 GMT-0400 (Eastern Daylight Time), which is not what you want.
You can combine a couple of existing StackOverflow answers to accomplish what you want:
// https://stackoverflow.com/a/23593099/378779
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('-');
}
// https://stackoverflow.com/a/13572682/378779
function getFstDayOfMonFnc() {
var date = new Date();
return new Date(date.getFullYear(), date.getMonth(), 1)
}
Assuming fdate should be the first of the month and tdate should be today's date:
document.getElementById('fdate').value = formatDate( getFstDayOfMonFnc() );
document.getElementById('tdate').value = formatDate( new Date() );
var date = new Date();
var monthStart = Date.UTC(date.getFullYear(), date.getMonth())
monthStart = toIsoDateString(monthStart);
console.log(monthStart);
var nextMonthStart = Date.UTC(date.getFullYear(), date.getMonth() + 1);
nextMonthStart = toIsoDateString(nextMonthStart);
console.log(nextMonthStart);
function toIsoDateString(utcDate) {
var date = new Date(utcDate);
return date.toISOString().split('T')[0];
}
I also had made such a program but there were a few changes, instead of just the first date I was getting the current time, the first day of the month and also the last date of the month.
This is the code I used:
<html>
<head>
<title>JavaScript Dates</title>
</head>
<body>
<script>
var date = new Date();
document.write("Current Date: " + date );
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
document.write("<br>"+firstDay);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
document.write("<br>"+lastDay);
</script>
</body>
</html>
You can edit this code and try working out.

Limit a date in input type date

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"/>

add getTime from date to another date without time javascript

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

Date Convert in jquery

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

Categories