0

I am trying to create date object without time info like 23/09/2019.

I already tried many solutions like;

new Date(d.setHours(0,0,0,0))
new Date(dateString).toUTCString().split(' ').slice(0, 4).join(' ')
new Date(d.getFullYear(), d.getMonth(), d.getDate())
new Date(new Date().getFullYear(),new Date().getMonth() , new Date().getDate())

However, all of this either give me string or date with time Tue Aug 27 2019 00:00:00 GMT+0300

I want to get this 23/09/2019 as a date object. When I write typeof result it should return date. I know js Date object stores date and time but Is it possible to get something like this?

hrrs
  • 66
  • 6
  • 5
    `d.toLocaleDateString()`? I mean, a date object will always output the date and time, but if you want just the date, then use the method that is there for just such a purpose – Jaromanda X Aug 27 '19 at 08:19
  • 1
    It will always contain time information, but you are not forced to use/print them. – sjahan Aug 27 '19 at 08:20
  • A solution may be in using a dedicated small library called "moment.js" which is very useful to manipulate dates : https://momentjs.com/ – EddiGordo Aug 27 '19 at 08:22
  • @EddiGordo I know moment.js but it also returns string not date object – hrrs Aug 27 '19 at 08:25
  • @hrrs if you want to display `dd/mm/yyyy`, you need to convert it to string. `new Date()` is a Date object and it has nothing to with `dd/mm/yyyy` or any other format – adiga Aug 27 '19 at 08:42
  • @adiga Yes you are right, I know it but I wonder if there is another way of getting this as date object. – hrrs Aug 27 '19 at 08:46
  • 1
    You can overwrite `Date.prototype.toString` with your own function: https://jsfiddle.net/khrismuc/fkhoys28/ – Chris G Aug 27 '19 at 08:46
  • It sounds like you have a problem and are approaching it from the wrong end. A Date object always contains time information, that’s part of the spec. If you want to print it without the time, use a string method that does so. If you want to compare dates without the time being a factor, use comparison methods for this (such as `moment(date).isSame(otherDate, ’day’)`). – Lennholm Aug 27 '19 at 08:47
  • Actually, there's no type for `Date`. If you want to check the type is `Date` object, you are in a wrong way. You may want to compare its instance using `instance of`. – Chase Choi Aug 27 '19 at 08:58
  • Date objects don't store date and time. They have an offset from the ECMAScript epoch in milliseconds (a time value), that's it. There are also some methods for getting various values, such as UTC and local date and time based on the time value and system timezone settings. – RobG Aug 27 '19 at 11:48

1 Answers1

-1

Have a look at Date - Javascript | MDN for Date methods to see more details. You can compose the date components however you like.

If you want the date/month/year, you just need to call those methods:

function zeroPad(n) {
  return n < 10 ? '0'+n : n;
}
var d = new Date();
var s = zeroPad(d.getDate()) + '/' + zeroPad(d.getMonth()+1) + '/' + d.getFullYear();
console.log(s);

See also getUTCDate, getUTCMonth, getUTCFullYear for UTC.

csum
  • 1,240
  • 8
  • 15