1.C语言中 有没有函数可以将字符串直接转为时间格式的
由于实际生活中,字符串形式的时间有可能有多种形式,比如月日年,或年月日,中间的分隔符也可能有所不同。所以C语言并没有提供此类的转换函数。
如果有需求,那么在确定字符串的组织格式前提下,可以自行书写一个转换函数。
有两种思路:
1 传入字符串,逐位解析每个字符,智能检查出数字之间的分隔符。然后根据分隔符,取出各个位上的数值,如年月日时分秒等。最终赋值到时间结构的对应成员变量上。
2 使用sscanf,根据约定好的格式,构建对应的格式字符串,将数值提取到对应的变量中。
对比二者,第一种方式代码量更大,但可以兼容更复杂的输入方式,使得输入更灵活,程序健壮性更好。第二种方式适用于严格约定输入格式的情况,以最少的代码量实现效果。
2.如何将一个字符串 转换为时间格式
将字符串类型的时间转换成date类型可以使用SimpleDateFormat来转换,具体方法如下:
1、定义一个字符串类型的时间;
2、创建一个SimpleDateFormat对象并设置格式;
3、最后使用SimpleDateFormat的parse方法将String类型的时间转换成Date类型的时间。
具体代码如下:
String string = "2014-3-17";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = dateFormat.parse(string);
System.out.println(date.toLocaleString().split(" ")[0]);//切割掉不要的时分秒数据
} catch (ParseException e) {
e.printStackTrace();
}
介绍一下SimpleDateFormat:SimpleDateFormat 是 Java 中一个非常常用的类,该类用来对日期字符串进行解析和格式化输出。
3.如何将字符串转换为时间格式
public static Date str2dateEx(String strValue) throws ParseException {
if (strValue == null)
throw new ParseException("字符串为空", 0);
Date theDate;
SimpleDateFormat theFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
theDate = theFormat.parse(strValue);
return theDate;
}
4.c#如何把8位字符串转换成日期格式
c#如何把8位字符串转换成日期格式?
方法1:
我们在处理入日期格式式常用DateTime.Pares()
但是这个形式的转换是相当有限的,有些C#是会不懂你写入的日期格式的如20031231大家都明白是2003-12-31号可以C#不认识他.我们可以这样子进行如下
//设置语言国家
System.IFormatProvider format=new System.Globalization.CultureInfo("zh-CN",true);
//指定转换格式
Response.Write(DateTime.ParseExact(this.TextBox1.Text,"yyyyMMdd",format));
这样他就能看出20031231号的日期格式了
不过可惜他只能够对yyyyMMdd形式进行不能对其它格式进行转换是不是美中不足?
不过.NET提供了另一种重载版本.
public static DateTime ParseExact(string, string[], IFormatProvider, DateTimeStyles);
可对指定的几种日期格式进行转换.
示例:
string strDateFormat = "yyyyMMdd";string date="20061141";
DateTime.ParseExact(date, strDateFormat, new CultureInfo("zh-CN"), DateTimeStyles.AllowWhiteSpaces);
不过具体的操作我想还是大家自己去试验一下效果可能会更好一些。
方法2:
日期必须是固定位数,20051126是8位,20050203就不能这么表示成200523
//分别获取20051126中的年、月、日字符串
string yyyy="20051126".Substring(0,4);
string mm="20051126".Substring(4,2);
string dd="20051126".Substring(5,2);
//拼写符合日期格式的字符串
string riqi=yyyy+"-"+mm+"-"+dd;
//将符合日期格式的字符串转化为DateTime数据类型
DateTime dt=Convert.ToDateTime(riqi);
5.如何将字符串转化为日期
format(cdate(x),"yyyy-m-d")
cdate是转换的函数
function FormatDateTime(const Format: string; DateTime: TDateTime): string;
overload;
当然和Format一样还有一种,但这里只介绍常用的第一种
Format参数是一个格式化字符串。DateTime是时间类型。返回值是一种格式化后的
字符串
重点来看Format参数中的指令字符
c 以短时间格式显示时间,即全部是数字的表示
FormatdateTime('c',now);
输出为:2004-8-7 9:55:40
d 对应于时间中的日期,日期是一位则显示一位,两位则显示两位
FormatdateTime('d',now);
输出可能为1~31
dd 和d的意义一样,但它始终是以两位来显示的
FormatdateTime('dd',now);
输出可能为01~31
ddd 显示的是星期几
FormatdateTime('ddd',now);
输出为: 星期六
dddd 和ddd显示的是一样的。
但上面两个如果在其他国家可能不一样。
ddddd 以短时间格式显示年月日
FormatdateTime('ddddd',now);
输出为:2004-8-7
dddddd 以长时间格式显示年月日
FormatdateTime('dddddd',now);
输出为:2004年8月7日
e/ee/eee/eeee 以相应的位数显示年
FormatdateTime('ee',now);
输出为:04 (表示04年)
m/mm/mmm/mmmm 表示月
FormatdateTime('m',now);
输出为:8
FormatdateTime('mm',now);
输出为 08
FormatdateTime('mmm',now);
输出为 八月
FormatdateTime('mmmm',now);
输出为 八月
和ddd/dddd 一样,在其他国家可能不同
yy/yyyy 表示年
FormatdateTime('yy',now);
输出为 04
FormatdateTime('yyyy',now);
输出为 2004
h/hh,n/nn,s/ss,z/zzz 分别表示小时,分,秒,毫秒
t 以短时间格式显示时间
FormatdateTime('t',now);
输出为 10:17
tt 以长时间格式显示时间
FormatdateTime('tt',now);
输出为10:18:46
ampm 以长时间格式显示上午还是下午
FormatdateTime('ttampm',now);
输出为:10:22:57上午 缉筏光禾叱鼓癸态含卡
大概如此,如果要在Format中加普通的字符串,可以用双引号隔开那些
特定义的字符,这样普通字符串中如果含特殊的字符就不会被显示为
时间格式啦:
FormatdateTime('"today is" c',now);
输出为:today is 2004-8-7 10:26:58
时间中也可以加"-"或"\"来分开日期:
FormatdateTime('"today is" yy-mm-dd',now);
FormatdateTime('"today is" yy\mm\dd',now);
输出为: today is 04-08-07
也可以用":"来分开时间
FormatdateTime('"today is" hh:nn:ss',now);
输出为:today is 10:32:23
标签:四会新闻,