Getting current date and time in JavaScript

I have a script that prints the current date and time in JavaScript, but the DATE is always wrong. Here is the code: It should print 18/04/2012 15:07:33 and prints 3/3/2012 15:07:33 Answer .getMonth() returns a zero-based number so to get the correct month you need to add 1, so calling .getMonth() in may will return 4 and not 5. So in your code we can use currentdate.getMonth()+1 to output the correct value. In addition: .getDate() returns the day of the month <- this is the one you want .getDay() is a separate method of the Date object which will

How can i set the date to the beginning of calendar

I want to set the time to the 1st January 0000 and time at 00:00:00 i tried to calculate a lot but i’m unable to set the date to the beginning point. I am setting the date with negative sign like this – new Date(-4000) but setting the year to 0 gives me only the 1900 so how can get to the perfect date of 1st Jan 0000 and time at 00:00:00. Answer The way you were doing you can also set it, to -62167219200000 it will return the same results as new Date(“0000-01-01”) Only will set the date to

JavaScript date variable is returning incorrect dates for string input

I am receiving a string containing a datetime from a web service that is formatted as dd/MMM/yyyy hh:mm:ss it is not possible to change the web service output to match this JavaScript applications needs. For the sake of simplicity I am replacing the web service data with hardcoded Strings that have their values below. dateOne is invalid (Used to be valid but recently has been proving difficult) dateTwo is valid but incorrect (Time change from 8 to 10 is correct based on time zone but my time zone is not 20 years in the past) If anyone could point me

Sort String Json Date

I am getting date values like that: “/Date(1560458281000)/” from an array of objects. I just want to order these dates by descending and ascending. I am open to any examples with pure JavaScript and/or moment.js By the way, hours and minutes are important. I will show it like 2014/10/29 4:50 This code doesn’t work. Answer You should be carefull with your sorted variable, it’s missing the const / let initializer, I would have written: And since your dates are in timestamp format you don’t even need to convert them to date to compare them, you can substract the numbers dicrectly.

Invalid date when parsing with locale it

I need to parse a date in the “it” locale with momentjs, and I’m doing this What I get is “invalid date” and I don’t understand why. Can you help me? Using the “en” locale (with the date written as 12/20/2018) all is ok Answer The below snippet will accomplish what you want. It takes moment’s date format for a given local and passes it to the constructor when creating a moment. With that said, the comments above raise a lot of good points and this is not a reliable way to be handling dates. For example, if someone in

How to show current time in JavaScript in the format HH:MM:SS?

Can you please tell me how to set time in this format HH:MM:SS .I want to set that this in an div ? Answer DEMO using javaScript only Update Updated Demo

How to parse a date in format “YYYYmmdd” in JavaScript?

This is a noob question: How to parse a date in format “YYYYmmdd” without external libraries ? If the input string is not in this format I would like to get invalid Date (or undefined if it will be easier). Answer Usage: UPDATE: As Rocket said, months are 0-based in js…use this if month’s aren’t 0-based in your string UPDATE: More rigorous checking for validity of date. Adopted HBP’s way to validate date.