How can I save a date, which can be partial (i.e. only a year, maybe a month), and display it later with the same specification?

I want users to specify a date, which may or may not include day and month (but will have at least a year.) The problem is that it is stored as a date-time in the database; the missing day / month will be saved as the default values โ€‹โ€‹and I will lose the original format and date value.

My idea was to save the actual format in the column as a row in addition to the datetime column. Then I could use a string column when I need to display the date and date-time for everything else. The drawback is that there is an additional column for each date column in the table that I want to display, and printing localized dates will not be so easy, since I cannot rely on the datetime value ... I will probably have to parse the row.

I hope I missed something, and it might be easier.

(Note. I use Rails if this is important for the solution.)

+3
source share
11 answers

Jhenzie, , , . 1 = , 2 = , 4 = , 8 = ( ), .

, , , - jhenzie , datetime.

+9

, . , db, , .

+3

, .

1 = 2 = 4 =

3 - , 6 - , 7 - . int

+2
+2

-? 2008 2008-8 2008-8-1 - , , ,

0

, , "", , . , ( "YYYY-mm" ..) .

0

, , , , ( ). , , :

  • , datetime;
  • boolean month, ;
  • boolean day, .

, , ( , ).

: , int !

0

Informix . , . .

0

. . Value .

0

The built-in time types represent a point in time. You can use built-in types and create a column for precision (Year, Month, Day, Hour, etc.), Or you can create your own date structure and use empty parts (or another invalid value) for empty parts.

0
source

For a ruby, at least you can use this gem - partial date https://github.com/58bits/partial-date

0
source

Source: https://habr.com/ru/post/1697100/


All Articles