How to Format the Date in MongoDB

If you have documents that store dates as Date objects, but you want to return them in a different format, you can use the $dateToString aggregate pipeline operator.

For example, you might want a date to be returned in mm/dd/yyyy format instead of the long ISODate() format that includes minutes, seconds, milliseconds, etc

The $dateToString operator converts the Date object to a string, and optionally allows you to specify a format for the resulting output.

Continue reading

Change the Date Format For the Current Session in SQL Server

Whenever you connect to SQL Server, a bunch of default settings are applied to your session. These include the language and the date format settings.

The date format is normally determined by your default language. For example, if your default language is us_english, then the default date format will probably be mdy, and the first day of the week will be day 7 (Sunday).

If you change your language, you the date format will implicitly be updated accordingly.

However, you still have the option of changing the date format without changing the language. To do this, you can use SET DATEFORMAT.

Continue reading

5 Ways to Get the Hour from a Date in MongoDB

MongoDB provides a good range of aggregation pipeline operators for working with dates, including operators that extract certain parts of dates, such as the year, month, day, hours, minutes, etc.

There are also a couple of MongoDB methods that enable you to iterate through a cursor, and apply a JavaScript function. You can therefore use JavaScript to extract date values and date parts, etc from a field as required.

This article presents 5 ways to return the hour portion from a Date object in MongoDB.

Continue reading