一、问题

 1、html页面中时间显示出错,数据库中时间是正确的。

原因:没有把DateTime转成String类型。

 2、  在C#中,发现不能直接使用ToString("yyyy-MM-dd HH:mm")这样的格式进行日期格式转换。

原因:在DB中,字段设置为Nullable<System.DateTime>,是可空的DateTime类型。

二、解决方案

  if (user.BirthDate.HasValue)
  {
    userInfo.BirthDate = user.BirthDate.Value.ToString("yyyy-MM-dd");
  }else
  {
    userInfo.BirthDate = string.Empty;
  }

三、完整案例

        public UserInfo GetUserNoPasswordByUserName(string userName)
{ var user= db.T_User.Select(d => new { Address = d.Address, BirthDate = d.BirthDate, Company = d.Company, Phone = d.Phone, RealName = d.RealName, TelPhone = d.TelPhone, TypeID = d.TypeID, UserID = d.UserID, UserName = d.UserName, UserStatus = d.UserStatus }).Where(d => d.UserName == userName).SingleOrDefault();
UserInfo userInfo = new UserInfo();
userInfo.Address = user.Address;
if (user.BirthDate.HasValue)
{
userInfo.BirthDate = user.BirthDate.Value.ToString("yyyy-MM-dd");
}else
{
userInfo.BirthDate = string.Empty;
}
userInfo.Company = user.Company;
userInfo.Phone = user.Phone;
userInfo.RealName = user.RealName;
userInfo.TelPhone = user.TelPhone;
userInfo.TypeID = user.TypeID;
userInfo.UserID = user.UserID;
userInfo.UserName = user.UserName;
userInfo.UserStatus = user.UserStatus; return userInfo; }

  

//自定义视图数据类
public class UserInfo
{
public int UserID { get; set; }
public string UserName { get; set; }
public Nullable<short> TypeID { get; set; }
public short UserStatus { get; set; }
// public string Password { get; set; }
public string RealName { get; set; }
public string Company { get; set; }
public string BirthDate { get; set; }
public string Phone { get; set; }
public string TelPhone { get; set; }
public string Address { get; set; }
}

  

   //数据实体类
public partial class T_User
{ public int UserID { get; set; }
public string UserName { get; set; }
public Nullable<short> TypeID { get; set; }
public short UserStatus { get; set; }
public string Password { get; set; }
public string RealName { get; set; }
public string Company { get; set; }
public Nullable<System.DateTime> BirthDate { get; set; }
public string Phone { get; set; }
public string TelPhone { get; set; }
public string Address { get; set; } }

  

转载来源:https://blog.csdn.net/baidu_34331194/article/details/68944906

Nullable<System.DateTime>日期格式转换 (转载)的更多相关文章

  1. C# MVC 用户登录状态判断 【C#】list 去重(转载) js 日期格式转换(转载) C#日期转换(转载) Nullable<System.DateTime>日期格式转换 (转载) Asp.Net MVC中Action跳转(转载)

    C# MVC 用户登录状态判断   来源:https://www.cnblogs.com/cherryzhou/p/4978342.html 在Filters文件夹下添加一个类Authenticati ...

  2. DateTime日期格式转换,不受系统格式的影响

    Application.Initialize;  with FormatSettings do  begin    ShortDateFormat := 'yyyy-mm-dd';    LongDa ...

  3. DateTime日期格式获取 分类: C# 2014-04-15 10:36 233人阅读 评论(0) 收藏

    c#.net 获取时间年月日时分秒格式 //获取日期+时间 DateTime.Now.ToString();            // 2008-9-4 20:02:10 DateTime.Now. ...

  4. Oracle日期格式转换,tochar(),todate()

    Oracle日期格式转换 本文主要介绍Oracle中的日期转换. 1. 日期转化为字符串 (以2016年10月20日为例) select to_char(sysdate,'yyyy-mm-dd hh2 ...

  5. Python 日期格式转换

    经常需要爬取网站上的时间信息,不同的网站又有不同的日期显示方式.而我需要将日期格式转化为一种特定的格式,所以为了简便和学习,记录下各种不同的日期格式转换. 日期格式化符号: %y :两位数的年份表示( ...

  6. ssm日期格式转换

    ssm日期格式转换 1      需求 前端传入字符串类型日期转化成java中的Date类型,存入数据库中;将数据库中的日期类型通过jstl标签在前端页面转换成字符串类型. 2      步骤 2.1 ...

  7. java 获取当前日期和特殊日期格式转换

     1.获取当前日期: package com.infomorrow.dao; import java.sql.Timestamp; import java.util.Calendar; import ...

  8. 【dateFormatSymbols】JAVA特殊日期格式转换

    记录:特殊日期格式转换,如将yyyyMMdd转为01MAY2019 public static final String DATE_VIP_FORMAT = "yyyyMMdd"; ...

  9. Java练习 SDUT-2246_时间日期格式转换

    时间日期格式转换 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 对于日期的常用格式,在中国常采用格式的是"年 ...

随机推荐

  1. J2CACHE 两级缓存框架

    概述 缓存框架我们有ehcache 和 redis 分别是 本地内存缓存和 分布式缓存框架.在实际情况下如果单台机器 使用ehcache 就可以满足需求了,速度快效率高,有些数据如果需要多台机器共享这 ...

  2. java框架学习_mybaties

    Mybatis第一天   框架课程 1. 课程计划 第一天: 1.Mybatis的介绍 2.Mybatis的入门 a) 使用jdbc操作数据库存在的问题 b) Mybatis的架构 c) Mybati ...

  3. 1.2万事开头hello world+交互+getpass、sys模块初识

    1.python的hello world: ①运行cmd-输入python-输入print (“hello world!”) ②创造.py的文本helloworld.py(后缀是为了告诉其他人)-输入 ...

  4. Nginx+Tomcat反向代理利用certbot实现https

    一.利用Let's Encrypt 免费生成HTTPS证书 1.下载安装certbot(Let's Encrypt ) 2.利用certbot生成证书 3.配置nginx的https证书 安装cerb ...

  5. 11g统计信息自动收集任务

    1.查看统计信息自动收集任务的开启情况. select client_name,status from dba_autotask_client; 2.查看自动收集任务各个窗口的开启情况. col op ...

  6. CPP之内存分配

    new & delete expression 1. Introduction A new expression allocates and constructs an object of a ...

  7. ubuntu环境下编译linux内核问题解决备忘

    在使用比较新的gcc编译内核时,经常遇到这个问题: 问题一: 提示: mkimage" command not found - U-Boot images will not be built ...

  8. spring boot集成 servlet自动注册的两种方式

    有两种方式:(两种方式同时存在时,@Bean优先@ServletComponentScan实例化,生成两个对象) 1)通过@ServletComponentScan类注解 扫描 带有@WebServl ...

  9. HDU 6020---MG loves apple(枚举)

    题目链接 Problem Description MG is a rich boy. He has n apples, each has a value of V(0<=V<=9). A ...

  10. 课程四(Convolutional Neural Networks),第二 周(Deep convolutional models: case studies) —— 1.Practice questions

    [解释] 应该是same padding 而不是 valid padding . [解释] 卷积操作用的应该是adding additional layers to the network ,而是应该 ...