Parsing a string to a date in JavaScript

The best string format for string parsing is the date ISO format together with the JavaScript Date object constructor. Examples of ISO format: YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS. But wait! Just using the “ISO format” doesn’t work reliably by itself. String are sometimes parsed as UTC and sometimes as localtime (based on browser vendor and version). The … Read more

Format JavaScript date as yyyy-mm-dd

Just leverage the built-in toISOString method that brings your date to the ISO 8601 format: let yourDate = new Date() yourDate.toISOString().split(‘T’)[0] Where yourDate is your date object. Edit: @exbuddha wrote this to handle time zone in the comments: const offset = yourDate.getTimezoneOffset() yourDate = new Date(yourDate.getTime() – (offset*60*1000)) return yourDate.toISOString().split(‘T’)[0]

Get month name from Date

Shorter version: const monthNames = [“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December” ]; const d = new Date(); document.write(“The current month is ” + monthNames[d.getMonth()]); Note (2019-03-08) – This answer by me which I originally wrote in 2009 is outdated. See David Storey’s answer for a better solution.

Java string to date conversion

That’s the hard way, and those java.util.Date setter methods have been deprecated since Java 1.1 (1997). Moreover, the whole java.util.Date class was de-facto deprecated (discommended) since introduction of java.time API in Java 8 (2014). Simply format the date using DateTimeFormatter with a pattern matching the input string (the tutorial is available here). In your specific … Read more

Is there any way to change input type=”date” format?

It is impossible to change the format We have to differentiate between the over the wire format and the browser’s presentation format. Wire format The HTML5 date input specification refers to the RFC 3339 specification, which specifies a full-date format equal to: yyyy-mm-dd. See section 5.6 of the RFC 3339 specification for more details. This … Read more

How to add 30 minutes to a JavaScript Date object?

Using a Library If you are doing a lot of date work, you may want to look into JavaScript date libraries like Datejs or Moment.js. For example, with Moment.js, this is simply: var newDateObj = moment(oldDateObj).add(30, ‘m’).toDate(); Vanilla Javascript This is like chaos’s answer, but in one line: var newDateObj = new Date(oldDateObj.getTime() + diff*60000); … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)