Javascript strptime() is adding one month extra - javascript

From an Spring boot project we are calling GraalVM for processing some rules written in JavaScript. The GraalVM version is 'org.graalvm.sdk=1.0.0-rc11'. When we are using strptime(), it's adding one month extra. Like for the date 24/02/2021, it is converted to 24/03/2021. Can you please let me know why this is happening?
Here source is passed as the source date like 24/02/2021
const return_date = new Date().strptime(source, format(source)).toIsoString()
NOTE: This is happening if we execute this on the last day of a month like on 31st January, other day it is giving me the expected date returned. So any idea why strptime() is behaving like this?

other than strptime() function, You can directly use this way:
const return_date = new Date(source).toISOString()

Related

How to get last day of a given year with moment?

I can hard-code the last day of a year because anyway it will always be 31st December. There are many solutions using date, js, jquery. But I'm working on an Angular project and hence my code is in typescript. My tech lead wants me to do this using moment. I'm not supposed to hard-code any date. Is there any built-in method provided by moment to fetch the last day of a given year. And I'm saying given year because in my case given year is dynamic. It can change anytime. I'm using endOf() method. I'm getting error with custom year. I mean:
const lastDayOfYear = moment().endOf('year') is working fine, but:
const lastDayOfYear = moment().endOf(2025) is giving me this error:
Argument of type '2025' is not assignable to parameter of type 'StartOf'
I also tried:
const lastDayOfYear = moment().endOf("2025");
year=2025; const lastDayOfYear = moment().endOf(year);
How will this method work? I've gone through the entire Moment docs. Or should I stick to Date of javascript. Something like this:
new Date(new Date().getFullYear(), 11, 31);
Please provide a solution.
PS: I want last date of a given year in MM-DD-YYYY format only.
Well if you really wanted to work that out with moment instead of using `12-31-${year}` you could instead use:
moment([year]).endOf('year').format('MM-DD-YYYY')
moment(date).endOf('year');
Where date is a Date somewhere in that year.
set current date in moment new Date()
const now = moment(new Date()).endOf('year');
alert(now);

Javascript new date from month and year string

I bumped into a situation where I'm getting a string indicating only the month and year of a date, and I need to create a Date object out of it. If I pass just the string, e.g. "February 2020" into a Date constructor, I strangely get back the the day of the previous month, i.e. in this case 2020-31-01. Thus, I need to always add 1 day to get the proper month in the Date object.
Here is the code to replicate:
var date_str = "February 2020";
var dt = new Date(date_str)
console.log(dt) // Returns : 2020-01-31T23:00:00.000Z (????)
dt.setDate(dt.getDate() + 1);
console.log(dt) // Returns : 2020-02-01T23:00:00.000Z
Any idea what the logic is behind this rather strange behaviour, or do I miss something here?
Update
Have accepted the first answer as being relevant, thus the main question is solved. However, just to add to the confusion: the code snippet I included runs as described with node. Using EXACTLY the same logic in a Vue.js application return the correct Date. Very strange!
"February 2020" is not a valid input according to the specification thus you should not rely on it to work.
You should convert your input to one that is according to spec and then decide whether you need local time or UTC.
Handling time(zones) is one of the hardest things in JavaScript and I strongly recommend that you do not try to reinvent the wheel here yourself as it is really easy to mess up.
Libraries like momentjs can help you here.
Actually you are passing February 2020 into the date Constructor , and its assumes the
date as 1 February 2020 thus it give the output as its UTC date which may be previous
day depending on your region
Use moment.js library, it will give perfect.
moment("February 2020").format('L')
"02/01/2020"

In JavaScript/AngularJS, I can't seem to remove the "T" in a date field from the DB?

I must be missing something very obvious, but I could use help. I have an Angular web app, which is getting data from a MS Web API. The DTO object has two date fields. When those fields come in, there is a "T" in the date field.
e.g. 2017-05-01T03:43:55
For whatever reason, I can't seem to get rid of that darn "T" when using the date. I'd tried using a date format both on the calendar control that displays the date data, and using the JavaScript format() command. I also tried using the Javascript replace() command to replace the "T" with a space. And I tried using Moment.js to create a new date (which seems to fail and give me an invalid date).
var startDate = moment(badge.startDate).format('yyyy-MM-dd HH:mm:ss');
var startDate = badge.startDate.format(''yyyy-MM-dd HH:mm:ss'');
var startDate = moment(badge.startDate).replace('T',' ');
What am I missing? How should I be doing this? As far as I can tell, any of these should work?
You can create separate filter for that as below.
return (input) ? $filter('date')("2017-05-01T03:43:55", "yyyy-MM-dd HH:mm:ss") : '';
You can change for of date according to your requirements. You can see details Below.
https://docs.angularjs.org/api/ng/filter/date
use, don't build what already exists!
First visit moment js this component is very helpful and has many options which you can do with a date.
var date = moment(yourDate).format('YYYY-MM-DD');
format has a lot of options, you can find it on moment js documents.

Moment.js keeps returning UTC instead of Local Date

I'm having what I hope/assume is a simple and silly issue getting Moment.js to return the local date.
I'm passing in this date/time string: 2015-02-19T06:32:00.001-05:00
Using this code:
var departureDateTime = "2015-02-19T06:32:00.001-05:00";
moment(departureDateTime).format("YYYY-MM-DD"); // returns 2015-02-19
That looks correct. However, if I inspect the time with the code below it will return the time in UTC, which will cause the date to be incorrect (a day in the future) at certain times during the day.
moment(departureDateTime).format("h:mma")); // 11:32am
What am I missing? I thought the default for Moment.js was to return the local time, so I'm confused as to why it wouldn't return 6:32am instead of 11:32am.
Thanks for any assistance you can offer.
A way to do it:
var dateStr = "2015-02-19T06:32:00.001-05:00";
moment(dateStr).utcOffset(dateStr).format("h:mma")
(From here: http://momentjs.com/docs/#/manipulating/utc-offset/)
Edit: The intended way to do it, according to the documentation identified by K_C, is to use parseZone:
var dateStr = "2015-02-19T06:32:00.001-05:00";
moment.parseZone(dateStr).format('h:mma');

How to add three days to a given date in JavaScript?

I have one text box. When I enter a date with the format MM/DD/YYYY in that textbox and click outside the textbox I need to display the result as MM/DD/YYYY+3 using JavaScript.
For example, if my date is 12/31/2013 then the result would be 01/03/2014.
Hope this link will help you to find the answer:
adding-number-of-days-to-an-entered-date-in-javascript.
Have a look at the excellent library moment.js with which you easily can add three days to your original date.
In your case it would be something like:
var input = moment(myTextBoxValue);
input.add('d', 3); // input is now 3 days later
I guess the easiest to program and best readable solution would be to use a dedicated library for handling dates, such as Moment.js.
There, you have got the add function which allows you to add an arbitrary amount of time to a given point in time. E.g.:
moment().add('days', 7);
If you use the moment function to parse the time entered, use that as your source value, call add on it, and return it as JavaScript Date using the toDate function, you get exactly what you want.
So basically it comes down to:
var sourceDate = moment(new Date(2013, 7, 8)),
targetDate = sourceDate.add('days', 3),
result = targetDate.toDate();
Then, result contains the Date object you wanted to have.
I'd prefer that over native JavaScript handling of Date, as it is way more readble and hence understandable.

Categories