PDA

View Full Version : استفاده از فرمت string در یک datetime فرمت



pedram_ns
شنبه 26 فروردین 1391, 10:39 صبح
با سلام
یک گرید ویو دارم که یکی از فیلدهاش مقدار انقضا آگهی رو نشون میده که در دیتابیس datetime تعریف شده. حالا می خوام کدی بنویسم که اگه مقدار این فیلد از تاریخ امروز کمتر بود جای نمایش تاریخ بنویسه "منقضی شد".
ولی چون این مقدار datetime هست دیگه مقدار استرینگ رو نمیگیره.

1. چطور میشه این کار رو کرد؟
2.وقتی با dataset مقدار این گرید رو پر میکنم مقدار فیلد تاریخ بصورت کامل (تاریخ و ساعت ) میاد. چطور فقط تاریخ رو نشون بدم.

این کدی که من استفاده میکنم:


adapter.Fill(ds, "ads")
Dim reader As OleDbDataReader
reader = cmd.ExecuteReader()
reader.Read()

Dim date_expiry As String = reader("date_expiry").ToString()

reader.Close()

Dim L As Integer
For L = 0 To ds.Tables("ads").Rows.Count - 1

If ds.Tables("ads").Rows(L)("date_expiry") < Date.Today Then
ds.Tables("ads").Rows(L)("date_expiry") = "منقضی شده"
Else
ds.Tables("ads").Rows(L)("date_expiry") = ShamsiDate(date_expiry)
End If

Next

d_derakhshani
شنبه 26 فروردین 1391, 11:17 صبح
می تونی تاریخ رو convert کنی به String که مقدار منقضی شده رو هم بتونی جاش به برنامه بفرستی

CONVERT(nvarchar(50), myDateTimeField)

اگه خواستی با substring می تونی فقط تاریخ رو بگیری:

SUBSTRING(CONVERT(nvarchar(50), myDateTimeField) , 0, 10)

pedram_ns
شنبه 26 فروردین 1391, 14:17 عصر
ممنون. راستش درست نگرفتم چی شد. ممکنه روی کدهای خودم یه توضیحی بدید که باید چطور بنویسم. در ضمن بانک اطلاعاتی من اکسسه.

d_derakhshani
شنبه 26 فروردین 1391, 14:42 عصر
تو کوئریت حتما یه همچین چیزی داری:
Select ..,date_expiry,... from table
بجای date_expiry دستور Convert که گفتم بزار. بعد در کد خودتون دیگه همه چی اوکی میشه

pedram_ns
شنبه 26 فروردین 1391, 16:54 عصر
ممنون من این کار رو کردم و به این صورت کوریم شد:


SELECT [ID], [title], CONVERT(nvarchar(50), date_expiry) FROM [ads] ...

ولی الان این ارور رو دارم:

System.Data.OleDb.OleDbException: Undefined function 'CONVERT' in expression.

همونطور که گفتم دیتابیسم اکسسه این فرقی در قضیه نمیکنه؟

d_derakhshani
شنبه 26 فروردین 1391, 17:20 عصر
خوب می گفتید Access چرا فرق می کنه. این دستور واسه SQL Server هست

Alghoochi
شنبه 26 فروردین 1391, 17:27 عصر
ممنون من این کار رو کردم و به این صورت کوریم شد:


SELECT [ID], [title], CONVERT(nvarchar(50), date_expiry) FROM [ads] ...

ولی الان این ارور رو دارم:

System.Data.OleDb.OleDbException: Undefined function 'CONVERT' in expression.

همونطور که گفتم دیتابیسم اکسسه این فرقی در قضیه نمیکنه؟



ds.Tables("ads").Rows(L)("date_expiry") = ShamsiDate(date_expiry)

تو این خط که می‌خوای تاریخ رو تبدیل کنی کافیه از کلاس تبدیل تاریخ خود دات‌نت استفاده کنی و تاریخ رو به روز و ماه و سال تبدیل و استفاده کنی.

pedram_ns
شنبه 26 فروردین 1391, 21:28 عصر
ممنون از همه ولی من به جایی نرسیدم بذارید یک مورد دیگه رو تو همین زمینه بگم.

من توی گریدویوم یک بخش (یک دکمه) برای تمدید آگهی دارم:


<asp:TemplateField HeaderText="تمدید">
<ItemTemplate>
<asp:ImageButton ID="AdsٍExtension" runat="server" ImageUrl="~/images/web/Extension.gif" CommandName="AdvExtension" CommandArgument='<%# Eval("ID") %>' ToolTip="تمدید آگهی" />
</ItemTemplate>
<ItemStyle Width="40px" />
</asp:TemplateField>

حالا در کدهام چطور بگم که اگر هر کدوم از رکوردهام تاریخ انقضاش تموم شده بود تصویر تمدید رو نشون بده و اگر هنوز زمان داشت این تصویر و دکمه تمدید نشون داده نشه؟

این کد بخش تشخیص تاریخ انقضا هست:


Dim adapter As New OleDbDataAdapter(cmd)
Dim ds As New DataSet()
adapter.Fill(ds, "ads")

Dim L As Integer
For L = 0 To ds.Tables("ads").Rows.Count - 1

If ds.Tables("ads").Rows(L)("date_expiry") < Date.Today Then

????

End If

Next

در اون بخش علامت ؟ تشخیص داده که اگر یکی از رکوردها تاریخ انقضاش تموم شده چیکار کنه. چه کدی باید بنویسم تا اون دکمه درون گریدویو نمایش داده نشه؟

pedram_ns
دوشنبه 28 فروردین 1391, 09:19 صبح
کسی نظری نداره؟

alonemm
دوشنبه 28 فروردین 1391, 12:37 عصر
باسلام:

برای نمایش تاریخ به صورت کامل یا فقط تاریخ به مثال زیر توجه کنید:

// This code example demonstrates the DateTime.ToLongDateString(),
// DateTime.ToLongTimeString(), DateTime.ToShortDateString(), and
// DateTime.ToShortTimeString() methods.

using System;
using System.Threading;
using System.Globalization;

class Sample
{
public static void Main()
{
string msg1 = "The date and time patterns are defined in the DateTimeFormatInfo \n" +
"object associated with the current thread culture.\n";

// Initialize a DateTime object.
Console.WriteLine("Initialize the DateTime object to May 16, 2001 3:02:15 AM.\n");
DateTime myDateTime = new System.DateTime(2001, 5, 16, 3, 2, 15);

// Identify the source of the date and time patterns.
Console.WriteLine(msg1);

// Display the name of the current culture.
CultureInfo ci = Thread.CurrentThread.CurrentCulture;
Console.WriteLine("Current culture: \"{0}\"\n", ci.Name);

// Display the long date pattern and string.
Console.WriteLine("Long date pattern: \"{0}\"", ci.DateTimeFormat.LongDatePattern);
Console.WriteLine("Long date string: \"{0}\"\n", myDateTime.ToLongDateString());

// Display the long time pattern and string.
Console.WriteLine("Long time pattern: \"{0}\"", ci.DateTimeFormat.LongTimePattern);
Console.WriteLine("Long time string: \"{0}\"\n", myDateTime.ToLongTimeString());

// Display the short date pattern and string.
Console.WriteLine("Short date pattern: \"{0}\"", ci.DateTimeFormat.ShortDatePattern);
Console.WriteLine("Short date string: \"{0}\"\n", myDateTime.ToShortDateString());

// Display the short time pattern and string.
Console.WriteLine("Short time pattern: \"{0}\"", ci.DateTimeFormat.ShortTimePattern);
Console.WriteLine("Short time string: \"{0}\"\n", myDateTime.ToShortTimeString());
}
}

/*
This code example produces the following results:

Initialize the DateTime object to May 16, 2001 3:02:15 AM

The date and time patterns are defined in the DateTimeFormatInfo
object associated with the current thread culture.

Current culture: "en-US"

Long date pattern: "dddd, MMMM dd, yyyy"
Long date string: "Wednesday, May 16, 2001"

Long time pattern: "h:mm:ss tt"
Long time string: "3:02:15 AM"

Short date pattern: "M/d/yyyy"
Short date string: "5/16/2001"

Short time pattern: "h:mm tt"
Short time string: "3:02 AM"

*/

alonemm
دوشنبه 28 فروردین 1391, 12:39 عصر
برای سوال بعدی که گفتید که چجوری تشخیص بدید که کدوم رکورد منقضی شده که عکس مورد نظر رو جای گزاری کنید:

شما میتونید در رویداد RowDataBound در کنترل گرید این امر رو پیاده سازی کنید.


به مثال زیر توجه کنید:



<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{

if(e.Row.RowType == DataControlRowType.DataRow)
{
// Display the company name in italics.
e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";

}

}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView RowDataBound Example</title>
</head>
<body>
<form id="form1" runat="server">

<h3>GridView RowDataBound Example</h3>

<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="true"
allowpaging="true"
onrowdatabound="CustomersGridView_RowDataBound"
runat="server">
</asp:gridview>

<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="CustomersSqlDataSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>


</form>
</body>
</html>