asp从数据库字段中取年月日

asp从数据库字段中取年月日,第1张

<%

'该函数作用:按指定参数格式化显示时间

'numformat=1:将时间转化为yyyy-mm-dd hh:nn格式。

'numformat=2:将时间转化为yyyy-mm-dd格式。

'numformat=3:将时间转化为hh:nn格式。

'numformat=4:将时间转化为yyyy年mm月dd日 hh时nn分格式。

'numformat=5:将时间转化为yyyy年mm月dd日格式。

'numformat=6:将时间转化为hh时nn分格式。

'numformat=7:将时间转化为yyyy年mm月dd日 星期×格式。

'numformat=8:将时间转化为yymmdd格式。

'numformat=9:将时间转化为mmdd格式。

function formatdate(shijian,numformat)

dim ystr,mstr,dstr,hstr,nstr '变量含义分别为年字符串,月字符串,日字符串,时字符串,分字符串

if isnull(shijian) then

numformat=0

else

ystr=DatePart("yyyy",shijian)

if DatePart("m",shijian)>9 then

mstr=DatePart("m",shijian)

else

mstr="0"&DatePart("m",shijian)

end if

if DatePart("d",shijian)>9 then

dstr=DatePart("d",shijian)

else

dstr="0"&DatePart("d",shijian)

end if

if DatePart("h",shijian)>9 then

hstr=DatePart("h",shijian)

else

hstr="0"&DatePart("h",shijian)

end if

if DatePart("n",shijian)>9 then

nstr=DatePart("n",shijian)

else

nstr="0"&DatePart("n",shijian)

end if

end if

select case numformat

case 0

formatdate=""

case 1

formatdate=ystr&"-"&mstr&"-"&dstr&" "&hstr&":"&nstr

case 2

formatdate=ystr&"-"&mstr&"-"&dstr

case 3

formatdate=hstr&":"&nstr

case 4

formatdate=ystr&"年"&mstr&"月"&dstr&"日 "&hstr&"时"&nstr&"分"

case 5

formatdate=ystr&"年"&mstr&"月"&dstr&"日"

case 6

formatdate=hstr&"时"&nstr&"分"

case 7

formatdate=ystr&"年"&mstr&"月"&dstr&"日 "&WeekdayName(Weekday(shijian))

case 8

formatdate=right(ystr,2)&mstr&dstr

case 9

formatdate=mstr&dstr

end select

end function

%>

这个是别人写好的日期格式化函数

在你用到的地方可以这样写

<%=formatdate(rs("time"),2)%>

还有就是自己可以用系统函数

<%

T=rs("time")

responsewrite Year(T) & "-" & Month(T) & "-" Day(T)

%>

看看我给你的格式化时间函数,里头就有你想要的格式,个人感觉这个函数已经非常完整了。你想要的效果也在里头有。

sql

server:

取年:

year(时间)

或者datepart(year,时间)

取月:

month(时间)或者datepart(month,时间)

oracle:

取年:

extract(year

from

时间)

或者

to_char(时间,'yyyy')

取月:

extract(month

from

时间)

或者

to_char(时间,'mm')

当然,上述前提都是你的时间是时间类型的,比如sql

server中是datetime类型,oracle中是date类型的

如果时间本来就是用字符串形式存储的,那直接根据格式用字符串函数处理就可以了

--取时间的某一个部分

select datepart(yy,getdate()) --year

select datepart(mm,getdate()) --month

select datepart(dd,getdate()) --day

select datepart(hh,getdate()) --hour

select datepart(mi,getdate()) --min

select datepart(ss,getdate()) --sec

因为这个时候time是datetime类型,left是针对字符串的 *** 作,先把time转换为字符串就可以了

select left(convert(varchar,getdate(),21),7)

year()函数是VBScript中用的,你现在要先使用SQL语句获取当前的年和月,当然要用T-SQL中的函数了,DATEPART ( datepart , date ) ,sql语句应该改为:sql="select from news where DATEPART(yy,infotime)="&syear&" and DATEPART(mm,infotime)="&smonth&"

以上就是关于asp从数据库字段中取年月日全部的内容,包括:asp从数据库字段中取年月日、SQL如何取年月、如何用getdate()获取月份等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9543341.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-29
下一篇2023-04-29

发表评论

登录后才能评论

评论列表(0条)

    保存