您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在日期的日期,月份或年份中添加数字

在日期的日期,月份或年份中添加数字

在.NET中,您可以使用以下AddMonths方法:@H_419_1@

@H_419_3@DateTime date = new DateTime(2013, 5, 19);
DateTime newDate = date.AddMonths(14);

至于使用指定格式从字符串中解析日期,则可以使用以下TryParseExact方法:@H_419_1@

@H_419_3@string dateStr = "19/05/2013";
DateTime date;
if (DateTime.TryParseExact(dateStr, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
    // successfully parsed the string into a DateTime instance =>
    // here we Could add the desired number of months to it and construct
    // a new DateTime
    DateTime newDate = date.AddMonths(14);
}
else
{
    // parsing Failed => the specified string was not in the correct format
    // you Could inform the user about that here
}
其他 2022/1/1 18:16:49 有442人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶