C# Adjust DateTime based on Input Parameter

Hi ... need your help with a DateTime issue.  I have a script that runs via a job scheduler. When the 'job' kicks off, it passes the appropriate DateTime (it is not always NOW.  The script runs 24 hours a day at the top of the hour.  Sometimes a job is delayed by 15 minutes to 45 minutes, but will always use the top of the hour time).

The DateTime are passed automatically to my script via @@startDateTime@@ and @@endDateTime@@.  The format of these two parameters are "yyyy-MM-dd 00:00".

Assume
     @@startDateTime@@ = "2016-02-10 10:00"
    @@endDateTime@@ = "2016-02-10 10:59"

I need to return newStartDateTime = "2016-02-10 09:00" and newEndDateTime = "2016-02-10 09:59"
evciAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dinesh manikkamCommented:
Are you trying to minus an hour in your C# code?
evciAuthor Commented:
Yes. I need to minus an hour.
thiyagu murugCommented:
Hi,

Here it is.
newStartDateTime = dt.AddHours(-1);
newEndDateTime = dt.AddHours(-1);

Open in new window

it_saigeDeveloperCommented:
In other words:
using System;
using System.Globalization;

namespace EE_Q28925390
{
	class Program
	{
		static string start = "2016-02-10 10:00";
		static string end = "2016-02-10 10:59";

		static void Main(string[] args)
		{
			Console.WriteLine("Start @ {0}", JobTime(start));
			Console.WriteLine("End @ {0}", JobTime(end));
			Console.ReadLine();
		}

		static DateTime JobTime(string value)
		{
			var result = default(DateTime);
			DateTime.TryParseExact(value, "yyyy-MM-dd hh:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out result);
			return result.AddHours(-1);
		}
	}
}

Open in new window

Produces the following output -Capture.JPGIf you need to have it formatted, then just use the ToString method with the appropriate format; e.g. -
using System;
using System.Globalization;

namespace EE_Q28925390
{
	class Program
	{
		static string start = "2016-02-10 10:00";
		static string end = "2016-02-10 10:59";

		static void Main(string[] args)
		{
			Console.WriteLine("Start @ {0}", JobTime(start).ToString("yyyy-MM-dd hh:mm"));
			Console.WriteLine("End @ {0}", JobTime(end).ToString("yyyy-MM-dd hh:mm"));
			Console.ReadLine();
		}

		static DateTime JobTime(string value)
		{
			var result = default(DateTime);
			DateTime.TryParseExact(value, "yyyy-MM-dd hh:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out result);
			return result.AddHours(-1);
		}
	}
}

Open in new window

Which now produces the following output -Capture.JPG-saige-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
evciAuthor Commented:
Thanks for the help.  Worked.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.