Trong bài trước tôi đã hướng dẫn cách ngày mai của ngày hiện tại, để cho đủ bộ bài này tôi sẽ hướng dẫn cách lấy ngày quá khứ của ngày hiện tại trong ngôn ngữ lập trình C#.
DateTime.AddDays()
Là một phương thức thuộc DateTime được sử dụng để thêm hoặc bớt ngày vào ngày được thiết lập. Hiểu đơn giản nêu bạn thêm tham số -1 thì bạn sẽ có ngày quá khứ của ngày thiết lập.
Cú pháp - Syntax
public DateTime AddDays(
double value
)
double value
)
Ví dụ - Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GetYesterday
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Today: {0}", DateTime.Today);
DateTime y = DateTime.Today.AddDays(-1);
Console.WriteLine("Yesterday: {0}", y);
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GetYesterday
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Today: {0}", DateTime.Today);
DateTime y = DateTime.Today.AddDays(-1);
Console.WriteLine("Yesterday: {0}", y);
Console.ReadLine();
}
}
}
Kết quả - Result
No comments:
Post a Comment