首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c# datetime数据修改解决方案

2013-10-09 
c# datetime数据修改如何修改datetime数据里面的数据,比如将毫秒改为1,月改为2,其余不变[解决办法]using S

c# datetime数据修改
如何修改datetime数据里面的数据,比如将毫秒改为1,月改为2,其余不变
[解决办法]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime dt = DateTime.Now;
            dt = new DateTime(dt.Year, 2, dt.Day, dt.Hour, dt.Minute, dt.Second, 1);
            Console.WriteLine(dt.ToString("yyyy-MM-dd hh:mm:ss.fff"));
        }
    }
}


2013-02-08 11:13:36.001
Press any key to continue . . .
[解决办法]
DateTime dt = DateTime.Now;
DateTime d = new DateTime(dt.Year, dt.Month, 2);
这样月就变成2月了
[解决办法]
引用:
不是增加,也不是创建,是修改啊


楼主是程序员么?怀疑中
你看看DataTime定义

public DateTime Date { get; }
        //
        // Summary:
        //     Gets the day of the month represented by this instance.
        //
        // Returns:
        //     The day component, expressed as a value between 1 and 31.
        public int Day { get; }
        //
        // Summary:
        //     Gets the day of the week represented by this instance.
        //
        // Returns:
        //     A System.DayOfWeek enumerated constant that indicates the day of the week
        //     of this System.DateTime value.
        public DayOfWeek DayOfWeek { get; }
        //
        // Summary:
        //     Gets the day of the year represented by this instance.
        //
        // Returns:
        //     The day of the year, expressed as a value between 1 and 366.
        public int DayOfYear { get; }
        //
        // Summary:
        //     Gets the hour component of the date represented by this instance.
        //
        // Returns:
        //     The hour component, expressed as a value between 0 and 23.
        public int Hour { get; }
        //
        // Summary:
        //     Gets a value that indicates whether the time represented by this instance
        //     is based on local time, Coordinated Universal Time (UTC), or neither.
        //
        // Returns:
        //     One of the System.DateTimeKind values. The default is System.DateTimeKind.Unspecified.


        public DateTimeKind Kind { get; }
        //
        // Summary:
        //     Gets the milliseconds component of the date represented by this instance.
        //
        // Returns:
        //     The milliseconds component, expressed as a value between 0 and 999.
        public int Millisecond { get; }
        //
        // Summary:
        //     Gets the minute component of the date represented by this instance.
        //
        // Returns:
        //     The minute component, expressed as a value between 0 and 59.
        public int Minute { get; }
        //
        // Summary:
        //     Gets the month component of the date represented by this instance.
        //
        // Returns:
        //     The month component, expressed as a value between 1 and 12.
        public int Month { get; }
        //
        // Summary:
        //     Gets a System.DateTime object that is set to the current date and time on
        //     this computer, expressed as the local time.
        //
        // Returns:
        //     A System.DateTime whose value is the current local date and time.
        public static DateTime Now { get; }
        //
        // Summary:
        //     Gets the seconds component of the date represented by this instance.
        //
        // Returns:
        //     The seconds, between 0 and 59.
        public int Second { get; }
        //
        // Summary:
        //     Gets the number of ticks that represent the date and time of this instance.
        //
        // Returns:
        //     The number of ticks that represent the date and time of this instance. The
        //     value is between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks.
        public long Ticks { get; }
        //
        // Summary:
        //     Gets the time of day for this instance.
        //
        // Returns:
        //     A System.TimeSpan that represents the fraction of the day that has elapsed
        //     since midnight.
        public TimeSpan TimeOfDay { get; }
        //
        // Summary:
        //     Gets the current date.


        //
        // Returns:
        //     A System.DateTime set to today's date, with the time component set to 00:00:00.
        public static DateTime Today { get; }
        //
        // Summary:
        //     Gets a System.DateTime object that is set to the current date and time on
        //     this computer, expressed as the Coordinated Universal Time (UTC).
        //
        // Returns:
        //     A System.DateTime whose value is the current UTC date and time.
        public static DateTime UtcNow { get; }
        //
        // Summary:
        //     Gets the year component of the date represented by this instance.
        //
        // Returns:
        //     The year, between 1 and 9999.
        public int Year { get; }



全都是get 没有set
所以你只有新建一个 然后改好了再赋值给原来的 不就修改了么
不要死脑筋么 呵呵呵
[解决办法]
引用:
难道就只能
mid1.AddMilliseconds(0 - mid1.Millisecond);
这么写也太2了吧


我说了,DateTime是只读的。什么叫只能?你这么写根本就不能改变DateTime。
DateTime dt = DateTime.Now;
dt.AddDays(1);
Console.WriteLine(dt); //还是10月8日

热点排行