English 中文(简体)
网上服务的日期展示问题
原标题:datetime display issue from webservice

我的客户一方在我的剩余网络服务中展示客户的时段时, Im着ult子。

    public MainWindow()
    {
        InitializeComponent();
        string uriGroups = "http://localhost:8000/Service/Student";
        XDocument xDoc = XDocument.Load(uriGroups);

        foreach(var node in xDoc.Descendants("Student"))
        {

            GroupBox groupbox = new GroupBox();
            groupbox.Header = String.Format(node.Element("StudentID").Value);
            groupbox.Width = 100;
            groupbox.Height = 100;
            groupbox.Margin = new Thickness(2);

            TextBlock textBlock = new TextBlock();
            textBlock.Text = String.Format(node.Element("FirstName").Value + " " + (node.Element("LastName").Value));
            textBlock.TextAlignment = TextAlignment.Center;

            TextBlock textBlock1 = new TextBlock();
            textBlock1.Text = String.Format(node.Element("TimeAdded").Value);
            textBlock1.TextAlignment = TextAlignment.Center;
            textBlock1.VerticalAlignment = VerticalAlignment.Bottom;

            StackPanel stackPanel = new StackPanel();
            stackPanel.Children.Add(groupbox);

            stackPanel.Children.Add(textBlock);
            stackPanel.Children.Add(textBlock1);
            stackPanel.Margin = new Thickness(10);

            MainArea.Children.Add(stackPanel);
        }

    }

And my service looks like this:

public class Student
{
    ....
            public DateTime TimeAdded;
        public string TimeAddedString
        {
            get
            {
                return this.TimeAdded.ToString("dd/MM/yyyy hh:mm:ss");
            }
        }

但是,产出似乎如此:

“enterography

Is there a way on my client side app code to truncate this or reformat it?

最佳回答

您可以把它带往时代,然后使用String.Format

这方面的一个例子是,你可以采用一种格式:

String.Format("{0:M/d/yyyy}", ((DateTime)node.Element("TimeAdded").Value))

也可以使用

((DateTime)node.Element("TimeAdded").Value).ToString("d");

我的假设是,Value返回object,但如果返回某一天,你可以放弃这些 cast子。

If you are getting a string into your client, then you will need to use DateTime.Parse

(DateTime.Parse(node.Element("TimeAdded").Value)).ToString("d");
String.Format("{0:M/d/yyyy}", DateTime.Parse(node.Element("TimeAdded").Value))
问题回答

You are using TimeAdded...but I think you should be using TimeAddedString

textBlock1.Text = String.Format(node.Element("TimeAdded").Value);

textBlock1.Text = String.Format(node.Element("TimeAddedString").Value);





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签