question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Date column returns a date string instead of a date object

See original GitHub issue

Issue type:

[ ] question [x] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [x] postgres [ ] sqlite [ ] sqljs [ ] react-native

TypeORM version:

[ ] latest [ ] @next [x] 0.2.6 (or put your version here)

If I set a date column with @CreateDateColumn({type:'Date'}) in an entity. The result contains a date string {created: "2018-05-18"} instead of a date object.

I think it’s a bug because a date time column @CreateDateColumn() would return a date object.

@Entity()
export class Rate {
  @PrimaryGeneratedColumn()
  readonly id: number;

  @CreateDateColumn({type:'date'})
  created: Date;

}

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:7
  • Comments:31 (5 by maintainers)

github_iconTop GitHub Comments

90reactions
Gadzycommented, Feb 6, 2020

This violates TypeScript type checking !

Let’s say we have a FooEntity and a FooService like this :

// foo.entity.ts
import {Entity, Column} from "typeorm";

@Entity()
export class FooEntity {

    @Column("date")
    someDate: Date;

}

// foo.service.ts
import { Connection, FindOneOptions, Repository } from 'typeorm'

import { FooEntity } from './foo.entity'

export class FooService {
  private readonly connection: Connection

  async fooMethod(options: FindOneOptions): Promise<FooEntity> {
    const { date } = await this.connection.getRepository(FooEntity).findOne(options)
    /*
      ...
    */
  }
}

The type of date is checked against Date at compile type and is instead string at runtime.

60reactions
pleerockcommented, Jul 2, 2018

Hi @qbodart, I don’t believe you 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Parsing a string to a date in JavaScript - Stack Overflow
Just concatenate your date string (using ISO format) with "T00:00:00" in the end and use the JavaScript Date() constructor, like the example below....
Read more >
Demystifying DateTime Manipulation in JavaScript - Toptal
Try to avoid creating a date from a string unless it is in ISO date format. Use the Date(year, month, date, hours, minutes,...
Read more >
datetime — Basic date and time types — Python 3.11.1 ...
Returns a string representation of the timedelta object as a constructor call with canonical attribute values. Notes: This is exact but may overflow....
Read more >
Convert a String to a Date object in TypeScript | bobbyhadz
Use the Date() constructor to convert a string to a Date object in TypeScript, e.g. const date = new Date('2024-07-21') . The Date()...
Read more >
Date - JavaScript - MDN Web Docs - Mozilla
Chrome Edge Date Full support. Chrome1. Toggle history Full support. Edge12. Toggle hist... @@toPrimitive Full support. Chrome47. Toggle history Full support. Edge15. Toggle hist... Date() constructor...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found