开发者

Subtract time from date using format string

开发者 https://www.devze.com 2023-04-05 20:12 出处:网络
If I have a date and time in a DateTime object, can I remove say 10 minutes, or 24 hours etc from the date and time using a format string?

If I have a date and time in a DateTime object, can I remove say 10 minutes, or 24 hours etc from the date and time using a format string?

So if I have 1/1/1990 12:30:00pm and I wanted to remove 1 hour from it, can I use a format string?

edit

i need to store diary entries and the user can select a reminder type. so 1 hour before hand. so then i'd like to store the format string in a db that i c开发者_Python百科an get and apply to a datetime to get the reminder date time


Something like this might do what you need. Not sure what you mean by subtracting an hour using a format string though.

DateTime dt = new DateTime(1990, 1, 1, 12, 30, 0);
string s = dt.Subtract(new TimeSpan(1, 0, 0)).ToString("MM/dd/yyyy hh:mm:ss tt")


Best way is to convert to a DateTime object and then back to the formatted string (if that's what you need):

var time = DateTime.Parse("1/1/1990 12:30:00pm");
time = time.AddMinutes(-10);
string timeString = time.ToString("MM/dd/yyyy hh:mm:ss tt");


You can use this in your query if you have..

DATE_SUB(date,INTERVAL expr unit) Example:

YourDate = DATE_SUB(1/1/1990 12:30:00pm, INTERVAL 1 HOUR)

YourDate = DATE_SUB(1/1/1990 12:30:00pm, INTERVAL 12 MINUTE)

YourDate = DATE_SUB(1/1/1990 12:30:00pm, INTERVAL 45 SECONDS)

0

精彩评论

暂无评论...
验证码 换一张
取 消