Made time formatting in query more flexible