Convert DateTime to ISO8601 for use in SharePoint CAML queries

Ever wanted to create a CML query and insert a DateTime value? No such luck, DateTime values cannot be inserted directly, you need to convert the DateTime value to ISO8601 format and there is no DateTime function to do that.

Luckily, SharePoint provides a function for you in a utility class. The function is called, descriptively enough, CreateISO8601DateTimeFromSystemDateTime.

Have a look:

DateTime dt = DateTime.Now;
string date = Microsoft.SharePoint.Utilities.SPUtility.CreateISO8601DateTimeFromSystemDateTime(dt);

Now you can insert your date string into CAML and go on to conquer the world.

Found this article valuable? Want to show your appreciation? Here are some options:

a) Click on the banners anywhere on the site to visit my blog's sponsors. They are all hand-picked and are selected based on providing great products and services to the SharePoint community.

b) Donate Bitcoins! I love Bitcoins, and you can donate if you'd like by clicking the button below.

c) Spread the word! Below, you should find links to sharing this article on your favorite social media sites. I'm an attention junkie, so sharing is caring in my book!

Pin It

Published by

Bjørn Furuknap

I previously did SharePoint. These days, I try new things to see where I can find the passion. If you have great ideas, cool projects, or is in general an awesome person, get in touch and we might find out together.

Leave a Reply

Your email address will not be published.