4

If I have a timestamp in the form: yyyy-mm-dd hh:mm:ss:mmm

How can I just extract the date from the timestamp?

For instance, if a timestamp reads: "2010-05-18 08:36:52:236" what is the best way to just get 2010-05-18 from it.

What I'm trying to do is isolate the date portion of the timestamp, define a custom time for it to create a new time stamp. Is there a more efficient way to define the time of the timestamp without first taking out the date, and then adding a new time?

sooprise
  • 22,657
  • 67
  • 188
  • 276

7 Answers7

9

DateTime.Parse("2010-05-18 08:36:52:236").ToString("yyyy-MM-dd");

Run CMD
  • 2,937
  • 3
  • 36
  • 61
  • Did you get a chance to read the second part of my question? If there is a preferred way to do this, I would really like to know. Otherwise, what you have written here seems legit, Thanks. – sooprise May 18 '10 at 13:41
  • Well what do you mean by 'more efficient' ? If you want it faster, you could compare different methods with a profiler ? I think the method Andomar suggested will be faster ? (The substring) – Run CMD May 18 '10 at 13:43
  • Gives a `String was not recognized as a valid DateTime.` exception on my machine – Andomar May 18 '10 at 13:43
  • Yeah you should use CultureInfo.InvariantCulture somewhere – Run CMD May 18 '10 at 13:46
7

You should use the DateTime type:

DateTime original = DateTime.Parse(str);
DateTime modified = original.Date + new TimeSpan(13, 15, 00);
string str = modified.ToString("yyyy-MM-dd HH:mm:ss:fff");

Your format is non-standard, so you'll need to call ParseExact instead of Parse:

DateTime original = DateTime.ParseExact(str, "yyyy-MM-dd HH:mm:ss:fff", CultureInfo.InvariantCulture);
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

You could use substring:

"2010-05-18 08:36:52:236".Substring(0, 10);

Or use ParseExact:

DateTime.ParseExact("2010-05-18 08:36:52:236", 
                    "yyyy-MM-dd HH:mm:ss:fff", 
                    CultureInfo.InvariantCulture)
        .ToString("yyyy-MM-dd");
Andomar
  • 232,371
  • 49
  • 380
  • 404
2
DateTime date;
if (DateTime.TryParse(dateString, out date))
{
   date = date.Date; // Get's the date-only component.
   // Do something cool.
}
else
{
   // Flip out because you didn't get a real date.
}
Lance
  • 5,655
  • 4
  • 30
  • 32
1

Get the .Date member on the DateTime

DateTime date = DateTime.Now;
DateTime midnightDate = date.Date;
Kevin McKelvin
  • 3,467
  • 1
  • 27
  • 27
0

use it like this:

var x = DateTime.Now.Date; //will give you midnight today

x.AddDays(1).AddTicks(-1); //use these method calls to modify the date to whats needed.
VoodooChild
  • 9,776
  • 8
  • 66
  • 99
  • DateTime is immutable. You can't modify the date - you're creating a new one, which you happen to be throwing away instead of storing in a variable. – Joel Mueller May 18 '10 at 20:26
0

The best (and fastest) way to do this is to convert the date to an integer as the time part is stored in the decimal part.

Try this:

select convert(datetime,convert(int, @yourdate))

So you convert it to an integer and then back to a data and voila, time part is gone.

Of course subtracting this result from the original value will give you the time part only.

Cobusve
  • 1,572
  • 10
  • 23