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);
Related
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()
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"
I am making a filter between two dates, Start date and End date, the filter works perfect, it brings the data but it does not bring the complete data and it is because when selecting the dates and converting them to the format, it converts them but one day remains.
This way I am converting the dates:
FiltrarPorFechas(incial, final) {
this.ListaUsuarios = [];
const IniDate = new Date(incial);
const EndDate = new Date(final);
}
associate an image with debug and conversion:
As shown in the image, the initial and final dates arrive at the method thus "2019-07-10" and "2019-07-31" but when I try to convert them it puts them one day less as shown in the image.
I have tried to use moment formatDate and it does not work, I do not understand why and I do not want to add one day.
Somebody could help me ?
You may be potentially having an issue with Timezone. You could reset the time to 00:00:00 either using moment.js or from a normal Date() object.
Now, since your aim is to compare the times, you can use the diff() available from moment.js to achieve this. Please find the sample code below
(function() {
initial = '2019-07-09';
initial_formatted = moment(new Date(`${initial} 00:00:00`));
final = '2019-07-31';
final_formatterd = moment(new Date(`${final} 00:00:00`));
console.log(initial_formatted.diff(final_formatterd, 'days'));
// moment(new Date(`${incial} 00:00:00`)).format('YYYY-MM-DD HH:mm:ss');
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js" integrity="sha256-4iQZ6BVL4qNKlQ27TExEhBN1HFPvAvAMbFavKKosSWQ=" crossorigin="anonymous"></script>
More info
Moment diff()
Date Object set() method
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.
I am sending two fields from client side date and time.
date will be in the format of YYYY-MM-DD i.e 2016-11-08 and time will be in the format of 05:30 PM or 09:45 AM.
I want to combine these two fields and create new field say added_datetime , this field going to be inserted inside MongoDB, so I want it to be in the form of Mongo Date Object so that I can use it for searching with date.
Tried some random things using moments.js but unable to get what I want.
As mentioned in a similiar question you can create a date object with
var date = new Date(datestring);
The code
var startDate = new Date("1900-1-1 8:20:00 PM");
which the original questioner supplied works in chrome and should also work in node since it's the same js engine. This seems to answer your question.
You can find more on dates in the MDN documentation and MongoDB documentation.