# Is there some method for time format without nil error?

**URL:** <https://forum.crystal-lang.org/t/is-there-some-method-for-time-format-without-nil-error/807>\
**Category:** Help & Support\
**Created:** [May 31, 2019, 2:30am UTC](https://forum.crystal-lang.org/t/is-there-some-method-for-time-format-without-nil-error/807 "2019-05-31T02:30:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhonglin](https://avatars.discourse-cdn.com/v4/letter/z/91b2a8/32.png) [@zhonglin](https://forum.crystal-lang.org/u/zhonglin)\
**Post date:** [May 31, 2019, 2:30am UTC](https://forum.crystal-lang.org/t/is-there-some-method-for-time-format-without-nil-error/807/1 "2019-05-31T02:30:38Z")

</div>

Hello, I am a newbie in crystal.  
I have a trouble to format time to string in render,  
I select the time field from db, we can say it `work_started_at`,  
and it should be (Time | Nil)  
then when I type

`work_started_at.to_s("%Y-%m-%d")`

The compiler said the error:

> no overload matches ‘Nil#to\_s’ with type String  
> Overloads are:
> 
> - Nil#to\_s(io : IO)
> - Nil#to\_s()
> - Object#to\_s(io : IO)
> - Object#to\_s()

How should I do with it?

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [May 31, 2019, 2:48am UTC](https://forum.crystal-lang.org/t/is-there-some-method-for-time-format-without-nil-error/807/2 "2019-05-31T02:48:57Z")

</div>

You would need to make sure the value returned from the database is not nil first. Most ORMs have a method to get the value as not nil when you know its not nil, for example in Granite `model.work_started_at!`, notice the `!`.

Another option of that isn’t a feature is to do like

```auto
if start = work_started_at
  # start is known to be not nil here
  # Then could do start.to_s
else
  # work_started_at is nil
end

```

---

<div class="post-metadata">

**Author:** ![zhonglin](https://avatars.discourse-cdn.com/v4/letter/z/91b2a8/32.png) [@zhonglin](https://forum.crystal-lang.org/u/zhonglin)\
**Post date:** [May 31, 2019, 1:02pm UTC](https://forum.crystal-lang.org/t/is-there-some-method-for-time-format-without-nil-error/807/3 "2019-05-31T13:02:43Z")

</div>

Thank you,  
Both worked.
