如何在GridView中更改日期时间 [英] How to change datetime in gridview

查看:281
本文介绍了如何在GridView中更改日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

现在,我正在构建一些网站,并使用GridView制作板.

在这里,我有一个问题.

当我从数据库中加载数据时,我想要这样显示DateTime.

如果我今天写点东西,那么只有15:42这样显示

明天,它的变化是这样的2015年8月16日(yyMMdd)

我该怎么做?

请给我您的绝妙想法或在互联网上分享一些示例.

谢谢.

我尝试过的事情:

只需使用基本的DateTime格式

Hello, everyone.

Now I`m building some websites and making a board using GridView.

Here, I have some question.

When I loaded the data from database, I want to show DateTime like this.

If I wrote something today, it showed like this 15:42 only time

And tomorrow, it changed like this 16/08/15 (yyMMdd)

How can I make it??

Please give me your brilliant idea or share some examples on the internet.

Thanks.

What I have tried:

Just using basic DateTime format

推荐答案

RydenChoi
我假设您正在使用C#进行编码.

请在下面看到此示例(在此处查看其示例: DateTime格式dotNetFiddle示例 [
Hi RydenChoi
I am making an assumption you are coding in C#.

Please see this example below (see it working here: DateTime format dotNetFiddle example[^])
using System;
using System.Collections.Generic;

namespace DateFormatTest
{
	public class Program
	{
		private const string TODAY_FORMAT = "HH:mm";
		private const string OTHER_DAY_FORMAT = "yyMMdd";
		
		private List<dataitem> _Data = new List<dataitem>
		{
			new DataItem { Id = 1, When = DateTime.Today.AddDays(-1) },
			new DataItem { Id = 2, When = DateTime.Now },
			new DataItem { Id = 3, When = DateTime.Today.AddDays(1) },
			new DataItem { Id = 4, When = null }
		};
		
		public void Main()
		{
			foreach(DataItem item in _Data)
			{
				Console.WriteLine("Id: " + Convert.ToString(item.Id) + ", When: " +FormatDate(item.When));
			}
		}
		
		// Helper method to format date depending on today or other day.
		private string FormatDate(DateTime? val)
		{
			return val.HasValue ? 
				((DateTime)val).Date == DateTime.Today ? ((DateTime)val).ToString(TODAY_FORMAT) : ((DateTime)val).ToString(OTHER_DAY_FORMAT)
				: "n/a";
		}
	}
			
			
	public class DataItem
	{
		public int Id { get; set; }
		public DateTime? When { get; set; }
	}
}



1.我的示例数据记录:"DataItem"类.注意,我已经创建了它来模拟您的数据记录.我的具有一个可为空的DateTime属性:何时".我已经创建了DataItem的硬编码列表来模拟数据库.

2.我为两种不同的日期或时间格式定义了两个字符串常量.

3.我已经实现了一个辅助方法"FormatDate",该方法采用DateTime类型的参数? (可为空).根据值,将应用固定日期格式,或者如果日期值为null,则返回"n/a".您可能希望返回其他内容.

4.通过DateTime吗?值传递给FormatDate方法,并根据需要进行操作.在我的示例程序中,它输出以下内容

ID:1,时间:160815
ID:2,时间:09:49
ID:3,时间:160817
ID:4,时间:n/a



1. My example data record: "DataItem" class. Notice I have created this to simulate your data record. Mine has a nullable DateTime property: "When". I have created a hardcoded list of DataItem to mock the database.

2. I have defined two string constants, for the two different date or time formats.

3. I have implemented a helper method "FormatDate" which takes parameter of type DateTime? (nullable). Depending on the value the constant date format is applied, or if the date value is null, then it returns ''n/a''. You may wish to return something different.

4. Pass the DateTime? value to the FormatDate method and do as you wish with it. In my example program it outputs the following

Id: 1, When: 160815
Id: 2, When: 09:49
Id: 3, When: 160817
Id: 4, When: n/a


这篇关于如何在GridView中更改日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆