DateTime to String

Need something like strtotime() in PHP

Page 1 of 1

6 Replies - 5866 Views - Last Post: 23 September 2008 - 02:04 PM Rate Topic: -----

#1 Moonbat  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 36
  • View blog
  • Posts: 424
  • Joined: 30-June 08

DateTime to String

Post icon  Posted 22 September 2008 - 04:07 PM

I was wondering, in PHP they have a function called strtotime(). You can use phrases like "next sunday" as parameters. So something like:
$year = date("Y", strtotime("this sunday"));

would be perfectly valid and would come out as the actual date for the next Sunday.

Does C# have a function that can parse strings like "next Sunday" and other ambiguous terms? I'm trying to countdown the time from today to an unknown date that occurs every Sunday, so I can't specifiy the exact date with numbers.

Is This A Good Question/Topic? 0
  • +

Replies To: DateTime to String

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 14039
  • View blog
  • Posts: 56,179
  • Joined: 12-June 08

Re: DateTime to String

Posted 23 September 2008 - 10:53 AM

Not that I know of.. but to do so would be a trivial function.. roll your own!
Was This Post Helpful? 0
  • +
  • -

#3 tody4me  Icon User is offline

  • Banned
  • member icon

Reputation: 13
  • View blog
  • Posts: 1,398
  • Joined: 12-April 06

Re: DateTime to String

Posted 23 September 2008 - 12:07 PM

There isn't a way to do what you're wanting directly, however you can get the current day of week and adding / subtracting the number of days depending on the day of week.

This post has been edited by tody4me: 23 September 2008 - 12:07 PM

Was This Post Helpful? 1
  • +
  • -

#4 Moonbat  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 36
  • View blog
  • Posts: 424
  • Joined: 30-June 08

Re: DateTime to String

Posted 23 September 2008 - 12:45 PM

View Posttody4me, on 23 Sep, 2008 - 12:07 PM, said:

There isn't a way to do what you're wanting directly, however you can get the current day of week and adding / subtracting the number of days depending on the day of week.

Thanks for the idea tody4me. It'll be a bit tedious, but I guess it has to be done.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon


Reputation: 7163
  • View blog
  • Posts: 14,929
  • Joined: 16-October 07

Re: DateTime to String

Posted 23 September 2008 - 01:21 PM

Oh sigh! :blink: You should be happy it's not all done for you, it gives you something the play with. A new path to explore.

What the hell is it with lazy PHP programmers today? :P

Here, enjoy:
DateTime dt = DateTime.Now;
dt = dt.AddDays(DayOfWeek.Sunday - dt.DayOfWeek); // prior or current sunday
dt = dt.AddDays(7); // next sunday
Console.WriteLine(dt);
Console.WriteLine(dt.Year);


Was This Post Helpful? 1
  • +
  • -

#6 tody4me  Icon User is offline

  • Banned
  • member icon

Reputation: 13
  • View blog
  • Posts: 1,398
  • Joined: 12-April 06

Re: DateTime to String

Posted 23 September 2008 - 01:24 PM

I guess there goes that snippet idea...

This post has been edited by tody4me: 23 September 2008 - 01:24 PM

Was This Post Helpful? 0
  • +
  • -

#7 Moonbat  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 36
  • View blog
  • Posts: 424
  • Joined: 30-June 08

Re: DateTime to String

Posted 23 September 2008 - 02:04 PM

View Postbaavgai, on 23 Sep, 2008 - 01:21 PM, said:

Oh sigh! :blink: You should be happy it's not all done for you, it gives you something the play with. A new path to explore.

What the hell is it with lazy PHP programmers today? :P

Here, enjoy:
DateTime dt = DateTime.Now;
dt = dt.AddDays(DayOfWeek.Sunday - dt.DayOfWeek); // prior or current sunday
dt = dt.AddDays(7); // next sunday
Console.WriteLine(dt);
Console.WriteLine(dt.Year);


Hey, PHP programmers aren't lazy! It's hard to wean oneself of strtotime() :D

But, nonetheless, thank you for the code :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1