Reputation: 318
In my C# console application project I'm using MongoDB.Driver.2.4.3 with connection to MongoDB 3.2.10.
How would I convert "playerInBrazil.birthdate" in my code which is defined as BsonDateTime (I believe this is in UTC) to my local datetime value (Eastern Standard Time)?
I was trying to do subtract operation(not allowed) and DateTime.ToLocalTime Method () but couldn't make it work.
static void Main(string[] args)
{
DateTime myTimeConvert = DateTime.Now;
var client = new MongoClient("mongodb://localhost:27017");
var DB = client.GetDatabase("football");
var players = DB.GetCollection<Player>("players");
var playersInBrazil = players.AsQueryable()
.Where(p => p.country == "Brazil");
foreach (var playerInBrazil in playersInBrazil)
{
Console.Write(playerInBrazil.firstname);
Console.Write(" birthdate in UTC time is ");
Console.Write(playerInBrazil.birthdate);
Console.Write(" and in my local time is ");
//myTimeConvert =?
Console.WriteLine(myTimeConvert);
}
}
internal class Player
{
public ObjectId Id { get; set; }
public string firstname { get; set; }
public BsonDateTime birthdate { get; set; }
public string country { get; set; }
public double goals { get; set; }
}
}
Upvotes: 0
Views: 3216
Reputation: 11
BsonDateTime has a .ToLocalTime() method that returns a DateTime
More info here http://api.mongodb.com/csharp/current/html/M_MongoDB_Bson_BsonDateTime_ToLocalTime.htm
Upvotes: 1