Hiển thị kết quả từ 1 đến 10 / 10
  1. #1
    Tham gia
    20-09-2009
    Bài viết
    59
    Like
    2
    Thanked 2 Times in 2 Posts

    hỏi về datetime trong C#

    cho em hỏi cách chuyển dữ liệu từ string sang datetime trong C#.
    em muốn tính khoảng cách thời gian giữa 2 ngày trong C#.Nhưng em thực hiện phép trừ thì cần phải convert nó sang datetime.
    nhưng nó chuyển sang datetime nó gặp 1 vấn đề là nếu em dùng cách này
    DateTime ngay=Convert.ToDateTime(Ngay1);
    thì nó chuyển sang định dạng theo cài dặt datetime trong máy
    Nhưng em muốn chuyển sang định dạng datetime là MM/DD/YYYY hơặc DD/MM/YYYY tùy em.mà không ảnh hưởng đến chuyện em đổi định dạng ngày giờ trong máy.Vậy em phải làm thế nào?
    Có cách Convert nào giúp em không?Mong các anh chị giúp em.
    Em cảm ơn
    Quote Quote

  2. #2
    Tham gia
    19-06-2007
    Bài viết
    136
    Like
    0
    Thanked 0 Times in 0 Posts
    Đơn giản mà bạn. DateTime tmp = DateTime.ToDay;
    tmp.AddDays(2); // 2 ngày của bạn đó
    DateTime là DateTime, sao có Convert được. định dạng dd/MM/yyyy hoặc MM/dd/yyyy -> chỉ là display thôi. Muốn hiển thị kiểu chi, còn tùy, nếu là xuất ra string -> tmp.ToString("dd/MM/yyyy"); -> ra kiểu dd/MM/yyyy, còn trên control, bạn vào thuộc tính của nó, xem có format cái để hiển thị không. thông thường là DisplayFormat -> set FormatString của nó.

  3. #3
    Tham gia
    15-01-2008
    Bài viết
    121
    Like
    0
    Thanked 1 Time in 1 Post
    Quote Được gửi bởi hieupxd2cntt View Post
    cho em hỏi cách chuyển dữ liệu từ string sang datetime trong C#.
    em muốn tính khoảng cách thời gian giữa 2 ngày trong C#.Nhưng em thực hiện phép trừ thì cần phải convert nó sang datetime.
    nhưng nó chuyển sang datetime nó gặp 1 vấn đề là nếu em dùng cách này
    DateTime ngay=Convert.ToDateTime(Ngay1);
    thì nó chuyển sang định dạng theo cài dặt datetime trong máy
    Nhưng em muốn chuyển sang định dạng datetime là MM/DD/YYYY hơặc DD/MM/YYYY tùy em.mà không ảnh hưởng đến chuyện em đổi định dạng ngày giờ trong máy.Vậy em phải làm thế nào?
    Có cách Convert nào giúp em không?Mong các anh chị giúp em.
    Em cảm ơn
    Để chuyển từ 1 string sang 1 kiểu datetime bạn định nghĩa 1 kiểu DateTime mới. Trong .Net hỗ trợ bạn các kiểu sau:
    PHP Code:
            public DateTime(long ticks);
            
    //
            // Summary:
            //     Initializes a new instance of the System.DateTime structure to a specified
            //     number of ticks and to Coordinated Universal Time (UTC) or local time.
            //
            // Parameters:
            //   ticks:
            //     A date and time expressed in 100-nanosecond units.
            //
            //   kind:
            //     One of the System.DateTimeKind values that indicates whether ticks specifies
            //     a local time, Coordinated Universal Time (UTC), or neither.
            //
            // Exceptions:
            //   System.ArgumentOutOfRangeException:
            //     ticks is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.
            //
            //   System.ArgumentException:
            //     kind is not one of the System.DateTimeKind values.
            
    public DateTime(long ticksDateTimeKind kind);
            
    //
            // Summary:
            //     Initializes a new instance of the System.DateTime structure to the specified
            //     year, month, and day.
            //
            // Parameters:
            //   month:
            //     The month (1 through 12).
            //
            //   day:
            //     The day (1 through the number of days in month).
            //
            //   year:
            //     The year (1 through 9999).
            //
            // Exceptions:
            //   System.ArgumentOutOfRangeException:
            //     year is less than 1 or greater than 9999.-or- month is less than 1 or greater
            //     than 12.-or- day is less than 1 or greater than the number of days in month.
            //
            //   System.ArgumentException:
            //     The specified parameters evaluate to less than System.DateTime.MinValue or
            //     more than System.DateTime.MaxValue.
            
    public DateTime(int yearint monthint day);
            
    //
            // Summary:
            //     Initializes a new instance of the System.DateTime structure to the specified
            //     year, month, and day for the specified calendar.
            //
            // Parameters:
            //   calendar:
            //     The System.Globalization.Calendar that applies to this System.DateTime. 
            //
            //   month:
            //     The month (1 through the number of months in calendar).
            //
            //   day:
            //     The day (1 through the number of days in month).
            //
            //   year:
            //     The year (1 through the number of years in calendar).
            //
            // Exceptions:
            //   System.ArgumentException:
            //     Specified parameters evaluate to less than System.DateTime.MinValue or more
            //     than System.DateTime.MaxValue.
            //
            //   System.ArgumentOutOfRangeException:
            //     year is not in the range supported by calendar.-or- month is less than 1
            //     or greater than the number of months in calendar.-or- day is less than 1
            //     or greater than the number of days in month.
            //
            //   System.ArgumentNullException:
            //     calendar is null.
            
    public DateTime(int yearint monthint dayCalendar calendar);
            
    //
            // Summary:
            //     Initializes a new instance of the System.DateTime structure to the specified
            //     year, month, day, hour, minute, and second.
            //
            // Parameters:
            //   month:
            //     The month (1 through 12).
            //
            //   day:
            //     The day (1 through the number of days in month).
            //
            //   minute:
            //     The minutes (0 through 59).
            //
            //   year:
            //     The year (1 through 9999).
            //
            //   hour:
            //     The hours (0 through 23).
            //
            //   second:
            //     The seconds (0 through 59).
            //
            // Exceptions:
            //   System.ArgumentException:
            //     Specified parameters evaluate to less than System.DateTime.MinValue or more
            //     than System.DateTime.MaxValue.
            //
            //   System.ArgumentOutOfRangeException:
            //     year is less than 1 or greater than 9999. -or- month is less than 1 or greater
            //     than 12. -or- day is less than 1 or greater than the number of days in month.-or-
            //     hour is less than 0 or greater than 23. -or- minute is less than 0 or greater
            //     than 59. -or- second is less than 0 or greater than 59.
            
    public DateTime(int yearint monthint dayint hourint minuteint second);
            
    //
            // Summary:
            //     Initializes a new instance of the System.DateTime structure to the specified
            //     year, month, day, hour, minute, and second for the specified calendar.
            //
            // Parameters:
            //   calendar:
            //     The System.Globalization.Calendar that applies to this System.DateTime. 
            //
            //   month:
            //     The month (1 through the number of months in calendar).
            //
            //   day:
            //     The day (1 through the number of days in month).
            //
            //   minute:
            //     The minutes (0 through 59).
            //
            //   year:
            //     The year (1 through the number of years in calendar).
            //
            //   hour:
            //     The hours (0 through 23).
            //
            //   second:
            //     The seconds (0 through 59).
            //
            // Exceptions:
            //   System.ArgumentException:
            //     Specified parameters evaluate to less than System.DateTime.MinValue or more
            //     than System.DateTime.MaxValue.
            //
            //   System.ArgumentNullException:
            //     calendar is null.
            //
            //   System.ArgumentOutOfRangeException:
            //     year is not in the range supported by calendar.-or- month is less than 1
            //     or greater than the number of months in calendar.-or- day is less than 1
            //     or greater than the number of days in month.-or- hour is less than 0 or greater
            //     than 23 -or- minute is less than 0 or greater than 59. -or- second is less
            //     than 0 or greater than 59.
            
    public DateTime(int yearint monthint dayint hourint minuteint secondCalendar calendar);
            
    //
            // Summary:
            //     Initializes a new instance of the System.DateTime structure to the specified
            //     year, month, day, hour, minute, second, and Coordinated Universal Time (UTC)
            //     or local time.
            //
            // Parameters:
            //   month:
            //     The month (1 through 12).
            //
            //   day:
            //     The day (1 through the number of days in month).
            //
            //   minute:
            //     The minutes (0 through 59).
            //
            //   year:
            //     The year (1 through 9999).
            //
            //   hour:
            //     The hours (0 through 23).
            //
            //   kind:
            //     One of the System.DateTimeKind values that indicates whether year, month,
            //     day, hour, minute and second specify a local time, Coordinated Universal
            //     Time (UTC), or neither.
            //
            //   second:
            //     The seconds (0 through 59).
            //
            // Exceptions:
            //   System.ArgumentException:
            //     The specified time parameters evaluate to less than System.DateTime.MinValue
            //     or more than System.DateTime.MaxValue. -or-kind is not one of the System.DateTimeKind
            //     values.
            //
            //   System.ArgumentOutOfRangeException:
            //     year is less than 1 or greater than 9999. -or- month is less than 1 or greater
            //     than 12. -or- day is less than 1 or greater than the number of days in month.-or-
            //     hour is less than 0 or greater than 23. -or- minute is less than 0 or greater
            //     than 59. -or- second is less than 0 or greater than 59.
            
    public DateTime(int yearint monthint dayint hourint minuteint secondDateTimeKind kind);
            
    //
            // Summary:
            //     Initializes a new instance of the System.DateTime structure to the specified
            //     year, month, day, hour, minute, second, and millisecond.
            //
            // Parameters:
            //   month:
            //     The month (1 through 12).
            //
            //   millisecond:
            //     The milliseconds (0 through 999).
            //
            //   day:
            //     The day (1 through the number of days in month).
            //
            //   minute:
            //     The minutes (0 through 59).
            //
            //   year:
            //     The year (1 through 9999).
            //
            //   hour:
            //     The hours (0 through 23).
            //
            //   second:
            //     The seconds (0 through 59).
            //
            // Exceptions:
            //   System.ArgumentOutOfRangeException:
            //     year is less than 1 or greater than 9999.-or- month is less than 1 or greater
            //     than 12.-or- day is less than 1 or greater than the number of days in month.-or-
            //     hour is less than 0 or greater than 23.-or- minute is less than 0 or greater
            //     than 59.-or- second is less than 0 or greater than 59.-or- millisecond is
            //     less than 0 or greater than 999.
            //
            //   System.ArgumentException:
            //     Specified parameters evaluate to less than System.DateTime.MinValue or more
            //     than System.DateTime.MaxValue.
            
    public DateTime(int yearint monthint dayint hourint minuteint secondint millisecond);
            
    //
            // Summary:
            //     Initializes a new instance of the System.DateTime structure to the specified
            //     year, month, day, hour, minute, second, and millisecond for the specified
            //     calendar.
            //
            // Parameters:
            //   calendar:
            //     The System.Globalization.Calendar that applies to this System.DateTime. 
            //
            //   month:
            //     The month (1 through the number of months in calendar).
            //
            //   millisecond:
            //     The milliseconds (0 through 999).
            //
            //   day:
            //     The day (1 through the number of days in month).
            //
            //   minute:
            //     The minutes (0 through 59).
            //
            //   year:
            //     The year (1 through the number of years in calendar).
            //
            //   hour:
            //     The hours (0 through 23).
            //
            //   second:
            //     The seconds (0 through 59).
            //
            // Exceptions:
            //   System.ArgumentException:
            //     Specified parameters evaluate to less than System.DateTime.MinValue or more
            //     than System.DateTime.MaxValue.
            //
            //   System.ArgumentNullException:
            //     calendar is null.
            //
            //   System.ArgumentOutOfRangeException:
            //     year is not in the range supported by calendar.-or- month is less than 1
            //     or greater than the number of months in calendar.-or- day is less than 1
            //     or greater than the number of days in month.-or- hour is less than 0 or greater
            //     than 23.-or- minute is less than 0 or greater than 59.-or- second is less
            //     than 0 or greater than 59.-or- millisecond is less than 0 or greater than
            //     999.
            
    public DateTime(int yearint monthint dayint hourint minuteint secondint millisecondCalendar calendar);
            
    //
            // Summary:
            //     Initializes a new instance of the System.DateTime structure to the specified
            //     year, month, day, hour, minute, second, millisecond, and Coordinated Universal
            //     Time (UTC) or local time.
            //
            // Parameters:
            //   month:
            //     The month (1 through 12).
            //
            //   millisecond:
            //     The milliseconds (0 through 999).
            //
            //   day:
            //     The day (1 through the number of days in month).
            //
            //   minute:
            //     The minutes (0 through 59).
            //
            //   year:
            //     The year (1 through 9999).
            //
            //   hour:
            //     The hours (0 through 23).
            //
            //   kind:
            //     One of the System.DateTimeKind values that indicates whether year, month,
            //     day, hour, minute, second, and millisecond specify a local time, Coordinated
            //     Universal Time (UTC), or neither.
            //
            //   second:
            //     The seconds (0 through 59).
            //
            // Exceptions:
            //   System.ArgumentOutOfRangeException:
            //     year is less than 1 or greater than 9999.-or- month is less than 1 or greater
            //     than 12.-or- day is less than 1 or greater than the number of days in month.-or-
            //     hour is less than 0 or greater than 23.-or- minute is less than 0 or greater
            //     than 59.-or- second is less than 0 or greater than 59.-or- millisecond is
            //     less than 0 or greater than 999.
            //
            //   System.ArgumentException:
            //     The specified time parameters evaluate to less than System.DateTime.MinValue
            //     or more than System.DateTime.MaxValue. -or-kind is not one of the System.DateTimeKind
            //     values.
            
    public DateTime(int yearint monthint dayint hourint minuteint secondint millisecondDateTimeKind kind);
            
    //
            // Summary:
            //     Initializes a new instance of the System.DateTime structure to the specified
            //     year, month, day, hour, minute, second, millisecond, and Coordinated Universal
            //     Time (UTC) or local time for the specified calendar.
            //
            // Parameters:
            //   calendar:
            //     The System.Globalization.Calendar that applies to this System.DateTime. 
            //
            //   month:
            //     The month (1 through the number of months in calendar).
            //
            //   millisecond:
            //     The milliseconds (0 through 999).
            //
            //   day:
            //     The day (1 through the number of days in month).
            //
            //   minute:
            //     The minutes (0 through 59).
            //
            //   year:
            //     The year (1 through the number of years in calendar).
            //
            //   hour:
            //     The hours (0 through 23).
            //
            //   kind:
            //     One of the System.DateTimeKind values that indicates whether year, month,
            //     day, hour, minute, second, and millisecond specify a local time, Coordinated
            //     Universal Time (UTC), or neither.
            //
            //   second:
            //     The seconds (0 through 59).
            //
            // Exceptions:
            //   System.ArgumentException:
            //     The specified time parameters evaluate to less than System.DateTime.MinValue
            //     or more than System.DateTime.MaxValue. -or-kind is not one of the System.DateTimeKind
            //     values.
            //
            //   System.ArgumentNullException:
            //     calendar is null.
            //
            //   System.ArgumentOutOfRangeException:
            //     year is not in the range supported by calendar.-or- month is less than 1
            //     or greater than the number of months in calendar.-or- day is less than 1
            //     or greater than the number of days in month.-or- hour is less than 0 or greater
            //     than 23.-or- minute is less than 0 or greater than 59.-or- second is less
            //     than 0 or greater than 59.-or- millisecond is less than 0 or greater than
            //     999.
            
    public DateTime(int yearint monthint dayint hourint minuteint secondint millisecondCalendar calendarDateTimeKind kind); 
    Ví dụ: chuyển 1 kiểu string -> DateTime
    DateTime myDateTime = new DateTime(int.Parse(ngay), int.Parse(thang), int.Parse(nam));

  4. #4
    Tham gia
    13-10-2008
    Location
    www.vnfox.com
    Bài viết
    462
    Like
    0
    Thanked 10 Times in 10 Posts
    Quote Được gửi bởi tranmanhdit View Post
    Đơn giản mà bạn. DateTime tmp = DateTime.ToDay;
    tmp.AddDays(2); // 2 ngày của bạn đó
    DateTime là DateTime, sao có Convert được. định dạng dd/MM/yyyy hoặc MM/dd/yyyy -> chỉ là display thôi. Muốn hiển thị kiểu chi, còn tùy, nếu là xuất ra string -> tmp.ToString("dd/MM/yyyy"); -> ra kiểu dd/MM/yyyy, còn trên control, bạn vào thuộc tính của nó, xem có format cái để hiển thị không. thông thường là DisplayFormat -> set FormatString của nó.
    Mình thấy bạn tranmanhdit trả lời đúng rồi đó .ToString("dd/MM/yyyy");

  5. #5
    Tham gia
    14-11-2009
    Bài viết
    7
    Like
    0
    Thanked 0 Times in 0 Posts
    Các bạn giúp mình cái này với. Mình muốn Convert 1 chuổi string sang kiểu DateTime nhưng mà không được.

    DateTime ngaysinh;
    ngaysinh = DateTime.Parse(txt_ngaysinhchinhxac.Text);

    Khi minh Debug thi chuoi chuyền vào là "09/01/2009". Sau khi Convert thì nó chuyển thành "09/01/2009 12:00:00 AM". Có cách Convert mà chỉ lấy đúng chuổi đã chuyền vào không. Tức là mình muốn sau khi Convert thì chỉ có "09/01/2009" thôi. Ah mà nhớ là lúc đó ngaysinh vẫn là kiểu DateTime nha.

    Trân Trọng

  6. #6
    Tham gia
    19-06-2007
    Bài viết
    136
    Like
    0
    Thanked 0 Times in 0 Posts
    DateTime là DateTime. Nó luôn có khúc phía sau. Thực tế không có vấn đề gì là rắc rối nếu nó có khúc phía sau. Chỉ có bạn là tối ý quá thôi, không biết cách vận dụng như thế nào kiến thức của bản thân

  7. #7
    Tham gia
    20-04-2010
    Location
    Hà Nôi
    Bài viết
    23
    Like
    0
    Thanked 0 Times in 0 Posts
    Bạn tham khảo, mình thường dùng cách này:
    /// <summary>
    /// Parse string to datetime
    /// </summary>
    /// <param name="strDateTime">input string</param>
    /// <param name="datetime">output datetime</param>
    /// <returns>true if success/ otherwise return false</returns>
    public static bool StringToDateTime(string strDateTime, out Nullable<DateTime> datetime)
    {
    datetime = null;
    try
    {
    datetime = DateTime.ParseExact(strDateTime, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture) ;
    return true;
    }
    catch { }
    return false;
    }

    /// <summary>
    /// Parse string to full datetime
    /// </summary>
    /// <param name="strDateTime">input string</param>
    /// <param name="datetime">output datetime</param>
    /// <returns>true if success/ otherwise return false</returns>
    public static bool StringToFullDateTime(string strDateTime, out Nullable<DateTime> datetime)
    {
    datetime = null;
    try
    {
    datetime = DateTime.ParseExact(strDateTime, "dd/MM/yyyy hh:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture) ;
    return true;
    }
    catch { }
    return false;
    }

  8. #8
    Tham gia
    09-10-2003
    Location
    HN
    Bài viết
    853
    Like
    0
    Thanked 6 Times in 6 Posts
    Mặc định kiểu datetime của hệ thống là tháng/ngày/năm. Do vậy khi nhập liệu cần lưu ý điểm này.
    ở vấn đề của bạn là việc nhập liệu kiểu ngày/tháng/năm và muốn convert thành kiểu datetime. Vậy ở đây ta dùng CultureInfo, để assign các thông tin cần thiết rồi convert.
    VD:
    System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("vi-VN");
    culture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
    DateTime temp = Convert.ToDateTime("18/08/1980", culture);

  9. #9
    Tham gia
    14-02-2009
    Bài viết
    13
    Like
    0
    Thanked 0 Times in 0 Posts
    cái "vi-VN " có ý nghĩa j vậy?

  10. #10
    Tham gia
    19-10-2002
    Bài viết
    610
    Like
    1
    Thanked 4 Times in 4 Posts
    Quote Được gửi bởi crykhohieu View Post
    cái "vi-VN " có ý nghĩa j vậy?
    Đó là ISO codes cho ngôn ngữ và quốc gia Việt Nam, được dùng để đặt tên cho bản địa Việt Nam.

Bookmarks

Quy định

  • Bạn không thể tạo chủ đề mới
  • Bạn không thể trả lời bài viết
  • Bạn không thể gửi file đính kèm
  • Bạn không thể sửa bài viết của mình
  •