COMMUNITY

Confusing about Date result

look at this js code

var date1= new Date(2021,7,1,0,0,0)             //[same as haxe target to js](https://try.haxe.org/#46a55899) 
console.log(date1.getTime()) //1627747200-000    

const date2 = new Date('2021-07-01T00:00:00');
console.log(date2.getTime())//1625068800-000

how to got the date2 result in haxe ?

There is Date.fromString but the formats it can take are different than js, so you’d have to do something like:

using StringTools;

class Test {
  static function main() {
    final date2 = Date.fromString("2021-07-01T00:00:00".replace("T", " "));
    trace(date2.getTime());
  }
}

month in Date constructor is zero based (see Date - Haxe 4.2.1 API)