3/2/2023 0 Comments Javascript time minus minutes![]() ![]() And please dig into why something is weird. Do you really want to write code that makes you dive into the ECMAScript specification to understand why it works? Stop it! Just write () like a sane person!Īnyhow, if you ended up learning something about this amazing, quirky and weird language, I’m glad. ![]() This article is why you shouldn’t do math with Date objects or instances. And that’s why you can subtract two Date objects. We have explained different addition and Subtraction with the example below: Add or Subtract Days: const date new Date() const additionOfDays 10 tDate(date.getDate() + additionOfDays) // For subtract use minus (-) console.log('New. In Javascript add or subtract different date parameters is as easy as other languages. When you call the valueOf method on a Date-object, you get the amount of milliseconds. Date and Time modification is a very common part of development. When you use the + operator as a unary operator, the toValue function is called as well. This means that when you add two Date items together, you call the toString method on both, while when you subtract the two, the toValue method is called. Turns out, due to a special case in the language specification, the Date object is treated in a different way than other objects when it comes to using the addition operator:Īll native ECMAScript objects except Date objects handle the absence of a hint as if the hint Number were given Date objects handle the absence of a hint as if the hint String were given. This doesn’t explain why we suddenly get a bunch of milliseconds when we just write + new Date(), though. For objects, that will return the string, and for dates, that will return a formatted, human readable date string (in our case, Tue 21:32:21 GMT +0200 (CEST) ). In the case of the addition operator, this operation calls the toString prototypal method. This leads to quite a few different quirks - some of which we’re about to touch on now - but it does makes sense of what happens to both the plain object and the Date instance in our previous instance.Īs previously mentioned, JavaScript tries to convert each operand to a primitive value (namely a string or a number) when you apply an operand to it. Turns out, the + operator is overloaded in JavaScript, which means it can both concatenate strings and add numbers. Now, this seems pretty consistent until you meet the addition operator. ![]() You might have come across it in your JavaScript adventures. you can convert Unix timestamp into conventional time by using new Date(). var tMinutes(date.getMinutes()-30) console.log(minute) //it will print the time and date according to the above condition in Unix-timestamp format. If it can’t make sense of the operation, it outputs that special value NaN. var datenew Date() //here I am using '-30' to subtract 30 minutes from the current time. It calls this special method which - depending on what kind of operand it is (and a few other things) - either tries to turn it into a String or a Number. When you try to use arithmetic operators with stuff that aren’t of the Number type, JavaScript tries to coerce that value into something it can work with. Let’ start with understanding the general concept of what’s happening. Now, let’s dig into why this happens! The why □ ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |