What format does .getDay() accept? - javascript

Ok, so what I want to do is to retrieve some info from an input element, which is of type date. I then store this in a .json file, and I parse it later on to retrieve the data upon program start. After that, I want to use the date.getDay() function to figure out what day that date falls on.
I have only seen examples using var d = new Date(), and something tells me that the 'format' is different when using new Datethan using document.getElementById("dateInput").value;
Anyone catch my drift?
To sum up, I want to be able to find the day from the values outputted by an input type = "date" element.

Date.getDay() does not accept the default type (String) that gets returned by the elem.value()-call.
You have to pass a Date.
So you can convert any legit string to a date using the "new Date()" constructor.
let dateString = "2018-03-08"
let dateFromString = new Date(dateString) // working
Please have a look at this example code:
let date = new Date(mydate.value)
let day = date.getDay()
console.log(day)
<input id="mydate" type="date" value="2018-03-08">
You could also find some helps in the docs:
Date JavaScript
JavaScript Date Reference

Well, Date.getDay() does not accept any input, it is an "instance method", if you will, of the Date class. This means a Date object must be instantiated before you can call getDay() on it.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay
I think what you're trying to do is something along these lines:
<input id="js-date" type="date" value="2018-03-08">
let el = document.getElementById('js-date')
let date = new Date(el.value)
alert(date.getDay())

Related

Get all the months and year from provided ISO Date to current month and year

Suppose I have ISO Date inside an object as:
const dataCreated = {"readDetail":'2020-09-17 14:23:26.978Z'}
Is it possible to I get all date from created date till todays date.
Expected O/P ->assunming current date is jan 2021 :
['2020-09-17','2020-10-17','2020-11-17','2020-12-17','2021-01-17']
I tried different searches but was unable to get find anything related to it. If anyone has any solution or in someway can guide me that would be really helpful. If any further information needed please let me know.
What you want to do is just fill your array while looping over the dates. And with each loop add one month to your original date.
Note that you have to to create a new Instance of Date before saving it in your array since JavaScript saves references.
function getDates() {
var date = new Date("2020-09-17 14:23:26.978Z");
var now = new Date();
var datearray = [new Date(date)];
while(date.setMonth(date.getMonth() + 1) < now) {
datearray.push(new Date(date));
}
console.log(datearray);
}
Contrary to your expected output I'm saving Date Objects in the array. This has the advantage of giving you more opportunities when working with the Array elements afterwards. This could easily be changed though by changing the line where the Date object is pushed to the array.

How to add time to a date in js

I get a date with string type from API and then I parse it to a date type so that I can use it for a count down.
I want to add 30 days to the date that I've got from API.
Here is the code that I parsed
const time = Date.parse("2020-12-30T18:35:43");
I've already read this question and I tried to implement it
Add 10 seconds to a Date
but react does not recognize the getDate
if you need more information, please let me know
You need to wrap your parsed date with a new Date()
const time = new Date(Date.parse("2020-12-30T18:35:43"));
// As mention by other comments, this is enough
// const time = new Date("2020-12-30T18:35:43");
time.setSeconds(time.getSeconds() + 10) // 1609349753000
setSeconds and getSeconds are method of the Date, you was trying to execute them on a number.
EDIT :
Answer can be found here
In a general way you should use date-fns for date manipulations ;)
you can also setDate to your existing date. Check following code.
const date = new Date("2020-12-30T18:35:43");
date.setDate(date.getDate() + 30);

new Date() changes hours problem _ JavaScript

I have this string date and I want to use it in a date type for querying in sequelize.
let date = "2019-08-08T12:53:56.811Z"
let startDate = new Date(date)
console.log(startDate)
->> 2019-08-07T12:53:56.811Z
when I am trying to insert to db.it changes with another hour.
let newTask = Task.create({ time_to_deliver: startDate})
console.log(newTask.time_to_deliver)
->> 2019-08-08 17:23:56
what is this? is it something about timezone and UTC time stuff?
I think if you will make the "startDate" key in your DB of 'Date' type instead of 'String' type it will work. I have checked this with MongoDB and it worked for me.
"startDate: {type: Date}"
according to #barbsan answer . sequelize changes the date to toLocalString() format. in querying return data it turns to the actual date.
you can simply use this javascript functions for better enhancement
var d = new Date("July 21, 1983 01:15:00");
var n = d.getDate(); //this will give you 21
there is also a similar function available - getMonth(),
First, get this date day and month and time [for the time you can use these functions => 1. getTime() and 2. new Date().toISOString()]
and then send these things separately and combine them whenever you want retrieve.
There is the also second approach;
First, convert your date and time into a timestamp and enter that timestamp into the database.
var myDate="26-02-2012";
myDate=myDate.split("-");
var newDate=myDate[1]+"/"+myDate[0]+"/"+myDate[2];
alert(new Date(newDate).getTime());

Can't Compare dates using jQuery UI's datepicker

Ok, so I am attempting to test if a date is older than today. I am using jQuery UI's Datepicker to parse the date and assign it to a variable:
//Get Date as String
var $strDate = $(".pmt-date").text();
//Parse Date
var $dtDate = $.datepicker.parseDate("mm/dd/yy", $strDate);
Then I get today's date and assign it to a variable:
//Get Today's Date
var $strToday $.datepicker.formatDate('mm/dd/yy', new Date());
var $tDate = $.datepicker.parseDate('mm/dd/yy', $strToday);
Now I would like to compare $dtDate with $tDate. This is what I have tried:
if($dtDate > $tDate)
{
alert("Payment Date is Greater");
}
else
{
alert("Today's Date is Greater");
}
When I test this, I ALWAYS get the alert "Today's Date is Greater". I can display my two date variables via an alert, and I see the dates in correct format. So why does this comparison fail to work when the parse is working correctly?
Assuming that the field with class "pmt-date" is the datepicker-controlled <input> element, you need to fetch its value with .val(), not .text().
var $strDate = $(".pmt-date").val();
Your next line of code refers to a variable called "$date", not "$strDate", so:
var $dtDate = $.datepicker.parseDate("mm/dd/yy", $strDate);
Once you've got that, you can just directly compare the Date objects:
if ($dtDate < new Date())
There's no need to turn a newly-constructed Date object into a string and then back into a date. I guess you're Date to string and back in order to strip off the time-of-day part of the date, so that's not really a bad way to do it.
In date comparisons, more than means the date comes after, and less than means the date comes before. Older than would imply that the date comes before, and thus you want to use less than
if($dtDate < $tDate)

GetFullYear() function does not come after setting date from calendar?

I got a calendar which I want to take birthday of user. Its id is "DatePicker"
var tarih = new Date();
tarih =($("#DatePicker").val());
alert(tarih);
When I run this code I get the date from calendar. The problem occurs when I try to get year which is selected at calendar:
var tarih = new Date();
tarih =($("#DatePicker").val());
alert(tarih.getFullYear());
The code above didn't work. I checked date functions at SO but couldn't find this kind of example.
You can request a Date instance directly;
var tarih = $("#datepicker").datepicker("getDate");
alert(tarih.getFullYear());
i don't know much about JQuery but most probably .val() method will return you a string, so its actually not a date object you have to convert it into a date object before using getFullYear method, you can use Date.parse() function to convert a string to date object

Categories

Resources