Javascript turn <c:out value into date - javascript

I am trying to turn my <c: out value into a Javascript date but when I try it I am constantly getting invalid date. This the string that I am trying to turn into a Javascript dateTime.
24-02-2021 17:34:27
I am getting this value by doing the following:
var d = ('<c:out value="${post.end}"/>');
And then I try changing it into a date by the following code:
var date1 = new Date(d);
console.log(date1);
And this is where I am getting the invalid date
Invalid Date
Now I'm not sure if this was because I have time at the end of the string, so I've also tried removing the time at the end by using substring to have the date string as:
24-02-2021
But yet this still has the same error. I have also replaced all the - with / so the date appears like this:
24/02/2021
What can I do to make sure that this date is a 'valid' date so I can use it within my code.

Your date string format is wrong. Please see the MDN reference for the Date built-in object.
You could do something like this:
d = d.split(' ')[0] // '24-02-2021'
const [day, month, year] = d.split('-')
const date1 = new Date(year, month, day)

Related

setDate() is using current date as basis instead of desired object

I'm trying to add days to a Date object, but the output is not as desired:
// THIS IS JUST A SIMPLIFIED EXAMPLE.
let date = new Date("2019-01-01 00:00:00")
let finalDate = new Date()
finalDate.setDate(date.getDate() + 10)
console.log(finalDate)
Desired output:
11/01/2019 00:00:00
Actual output:
31/08/2019 13:06:30
It's using the current system date as a base and setting it to finalDate. which is not what I'm looking for.
The way you were declaring your literal date was in error. Also, you're better passing the existing date as a parameter to the constructor for the second one.
let date = new Date("2019-01-01 00:00:00");
let finalDate = new Date(date);
finalDate.setDate(date.getDate() + 10);
console.log(finalDate)
If your desired output is 11/01/2019 then you need to change a few things in how you're calculating your dates.
Here's code that get you what you're looking for:
let date = new Date('2019/01/01 00:00:00');
let finalDate = date;
finalDate.setMonth(date.getMonth() + 10);
console.log(finalDate);
Notice that for finalDate, I'm not setting it to a new instance of a date, but rather assigning it the value of the date variable. This way the two are the exact same date and allows us to begin adding months to the one we wish to add months to. Otherwise the days may not come out the same by initializing finalDate as its own separate date object.
Also notice that I'm calling getMonth rather than getDate, since we're adding months strictly.
Here is a working jsfiddle of your desired results:
https://jsfiddle.net/yzmk61xf/#&togetherjs=sSETlrppq6

How to add days to current date in angular

Using this:
var myDate = new Date(new Date().getTime()+(5*24*60*60*1000));
I get a date and time like this 2018-12-30T14:15:08.226Z, but only i want is this 2018-12-30. How can I retrieve just the date?
**This is Fixed. Thank You everyone who helps!!!
You're experiencing a JS problem, it has nothing to do with Angular.
This will use Date methods to get all the data you want:
const date = new Date ();
let dateString = `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`;
// 2018-12-26
You can take advantage of the fact that the ISO 8601 format (what you are getting by implicitely converting to string) is well-codified with a separator T between the date and time in order to split it.
toISOString() gives you what you're seeing. split("T") splits the string into an array of strings with T as separator. [0] then extracts the first element.
var myDate = new Date(new Date().getTime()+(5*24*60*60*1000));
console.log(myDate.toISOString().split("T")[0]);

Formatting system date, and comparing error

I have a String called yourDate 2012-11-30. Now i need to compare this date with the system date to check if it has exceeded or equaled the system date.
How can i do this ?
My approach;
var tDay= Ext.Date.parse("2012-11-30", "Y-m-d");
var sDay= new Date().dateformat('Y-m-d'); // system date
I get an error message TypeError: (new Date).dateformat is not a function. How can i correct this ?
note: I am using Extjs
Try the following:
var sDay = Ext.Date.format(new Date(), 'Y-m-d')
But I don't think you need to format your current date for comparison.
You can just use var sDay = new Date() and then compare date objects like if(sDay > tDay)

How to format JSON date?

so, i need format JSON date from this format
"9/30/2010 12:00:00 AM", it is MM/DD/YYYY HH:MM:SS to format like this : DD/MM/YYYY, so i dont need info about hours, min and sec, and i need replace months and days from json, i tried some different ways but it always failed
i need do this using jQuery
also i didnt find any answer to formating this date type, all i found was formating date like this :/Date(1224043200000)/
so anyone have idea?
you can create a Date Object from a string like so:
var myDate = new Date(dateString);
then you can manipulate it anyway you want, one way to get your desired output is:
var output = myDate.getDate() + "\\" + (myDate.getMonth()+1) + "\\" + myDate.getFullYear();
you can find more at this elated.com article "working with dates"
Unfortunately your "from" dateformat is not the one which is implementation-independent in JavaScript. And all the other formats depends on the implementation, which means even if this format would be understood by most of the implementation I/you can't be sure for example how the DD and MM order would be parsed (I am almost sure it would be local regional settings dependent). So I would recommend to use a 3rd party (or your hand written) date parser to get a Date object out of your input string. One such parser you can find here:
http://www.mattkruse.com/javascript/date/
Because your question is not 100% clear for me, it's possible that you have your date in the format of /Date(number)/ which suggests that you are calling an ASP.Net service from your jQuery code. In this case during the JSON parse you can convert it to a Date object:
data = JSON.parse(data, function (key, value) {
// parsing MS serialized DateTime strings
if (key == '[NAME_OF_DATE_PROPERTY_IN_THE_JSON_STRING]') {
return new Date(parseInt(value.replace("/Date(", "").replace(")/", ""), 10));
// maybe new Date(parseInt(value.substr(6))) also works and it's simpler
}
return value;
});
The code below solved my problem:
var date = new Date(parseInt(d.data[i].dtOrderDate.replace("/Date(", "").replace(")/", ""), 10));
var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();
Try something like this :
var date = new Date(parseInt(jsonDate.substr(6)));
where jsonDate is variable that stores your date

How to create object of Date("23.03.2010")

I have astring directly coming form the database and I am creating object of Date as
Date dt=Date("23.03.2010") and it is comin NaN
whereas when I use Date dt= Date("03/23/2010") it works fine.
Any Idea how I can get this working?.
You can parse the string from the database and then create the date object. You will have to subtract 1 from the parsed month value to get a correct date.
var dateString = "23.03.2010";
var dateParts = dateString.split(".");
var dt = new Date(dateParts[2], dateParts[1] - 1, dateParts[0]);
You must pass string (parsed) dates in MDY format. This is to prevent ambiguity (does 5/6/2010 mean 6th May or 5th June?)
If you prefer, you can use new Date(year, month, day) format, and pass the arguments separately.
The safest way if is you can return the date as milliseconds since 1970-01-01, then you can easily create a Date object from it. Example:
var n = 1269302400000;
var dt = new Date(n);
Note that you'll want to invoke Date with the new operator - from the Mozilla Developer Center:
Invoking Date in a non-constructor
context (i.e., without the new
operator) will return a string
representing the current time.
The same page details the syntax of the Date constructor.
If you are constructing a Date from a string the format accepted is governed by the rules of the Date.parse method. See Microsoft's Date.parse documentation for a summary of these rules.
Give this a try...
var dateParts = '23.03.2010'.split('.');
// -1 from month because javascript months are 0-based
var dateObj = new Date(dateParts[2], dateParts[1]-1, dateParts[0]);
try
d="23.03.2010".split(".");
Date dt=Date([d[1],d[0],d[2]].join("/"))
i think it isn't the most beautiful way.

Categories