0

I have the code (19 is the day, 6 is the month)

var dateObj = new Date("19.6.2018");
var month = dateObj.getUTCMonth() + 1; //months from 1-12
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();

newdate =  day + '.' + month + '.' + year;
alert(newdate);

This code returns NaN.NaN.NaN

Function getUTCDate() returns a month. I do not know why.

I'm Slovak. The first number is the day. The second number is a month.

  • 3
    `dateObj` is invalid date – Adelin Jun 19 '18 at 07:17
  • I'm Slovak. The first number is the day. The second number is a month. I do not know how to exchange a day with a month –  Jun 19 '18 at 07:18
  • `dateObj` would be null. check it with `console.log(dateObj)` etc – Muhammad Usman Jun 19 '18 at 07:21
  • var dateObj = new Date("6/19/2018"); – madflow Jun 19 '18 at 07:22
  • Nope, the above could also be fine, if he enters the month before day. – Rehan Umar Jun 19 '18 at 07:22
  • How to exchange a day with a month? Date I get from input where is the first day –  Jun 19 '18 at 07:23
  • Why not you use `new Date(year, month, day)`. Doc : [w3schools](https://www.w3schools.com/js/js_dates.asp). But these will in `int` not `String`. – AA Shakil Jun 19 '18 at 07:27
  • 1
    Possible duplicate of [how to parse a "dd/mm/yyyy" or "dd-mm-yyyy" or "dd-mmm-yyyy" formatted date string using JavaScript or jQuery](https://stackoverflow.com/questions/10430321/how-to-parse-a-dd-mm-yyyy-or-dd-mm-yyyy-or-dd-mmm-yyyy-formatted-date-stri) – Talha Junaid Jun 19 '18 at 07:34

2 Answers2

4

The dateString argument to Date must be in a format recognised by Date.parse()

String value representing a date. The string should be in a format recognized by the Date.parse() method (IETF-compliant RFC 2822 timestamps and also a version of ISO8601).

The date time string may be in a simplified ISO 8601 format. For example, "2011-10-10"

In your case it can be

var dateObj = new Date("2018-06-19");
var month = dateObj.getUTCMonth() + 1; //months from 1-12
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();

newdate =  day + '.' + month + '.' + year;
console.log(newdate);

You can take you string and format it properly like

const str = "19.6.2018"
const arr = str.split('.');
const newString = `${arr[1]}-${arr[0]}-${arr[2]}`;
console.log(newString)
var dateObj = new Date(newString);
console.log(dateObj)
    var month = dateObj.getUTCMonth() + 1; //months from 1-12
    var day = dateObj.getUTCDate();
    var year = dateObj.getUTCFullYear();

    newdate =  day + '.' + month + '.' + year;
    alert(newdate);
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
0

In addition to @Shubham Khatri answer, here's how you can convert your input to the correct format:

const getUTCDate = (year, month, day ) => {
  const date = new Date(`${year}-${month}-${day}`);
  const m = date.getUTCMonth() + 1; //months from 1-12
  const d = date.getUTCDate();
  const y = date.getUTCFullYear();

  return [m, d, y]
}

const input = '19.6.2018'
const date = input.split('.')
// Here we convert your input, in the correct order `getUTCDate` expected
const result = getUTCDate(date[2], date[1], date[0])
console.log(result)
Jordan Enev
  • 16,904
  • 3
  • 42
  • 67