如何将日期时间转换为字符串?

时间:2014-08-06 22:32:31

标签: c# wpf

 private void calendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
    {

        // ... Get reference.
        var calendar = sender as Calendar;

        // ... See if a date is selected.
        if (calendar.SelectedDate.HasValue)
        {

            DateTime date = calendar.SelectedDate.Value;
            this.Title = date.ToShortDateString();
            Stream stream = File.Open(Convert.ToString(date+".txt"), FileMode.Open);
            textbox.Visibility = Visibility.Visible;
            textbox.Text = "";
            stream.Close();

        }


    }

如果用户点击日历,我想创建一个新的文本文件。文本文件的名称应该是用户单击的日期。

Stream stream = File.Open(Convert.ToString(date+".txt"), FileMode.Open);

此行生成System.NotSupportedException

编辑:我认为整个代码会有更多帮助

XAML

<Window x:Class="Terminkalender.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Kalender" Height="350" Width="525" >
<Grid>
    <Calendar  SelectedDatesChanged="calendar_SelectedDatesChanged" Name="calendar" Background="Orange" HorizontalAlignment="Left" VerticalAlignment="Top" Height="310" Width="178" RenderTransformOrigin="0.528,0.769"/>
    <TextBox Name="textbox" AcceptsReturn="True" Visibility="Hidden" HorizontalAlignment="Left" Height="149" Background="Aqua" Margin="245,10,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="248"/>
    <!--SelectedDatesChanges erstellet eine Methode die im Falle eines geklickten Datum ein Ereignis ausführt-->
</Grid>

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO; 

namespace Terminkalender
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {


    public MainWindow()
    {
        InitializeComponent();
    }

    private void calendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
    {

        // ... Get reference.
        var calendar = sender as Calendar;

        // ... See if a date is selected.
        if (calendar.SelectedDate.HasValue)
        {

            // ... Display SelectedDate in Title and change Visibility of textbox
            DateTime date = calendar.SelectedDate.Value;
            this.Title = date.ToShortDateString();
            Stream stream = File.Open(Convert.ToString(date + ".txt"), FileMode.Open);
            textbox.Visibility = Visibility.Visible;
            textbox.Text = "";
            stream.Close();
        }


    }


}

}

4 个答案:

答案 0 :(得分:1)

试试这个

Stream stream = File.Open(Convert.ToString(date) +".txt", FileMode.Open);

您的".txt"是一个字符串,位于将非弦转换为字符串的函数内。

为了保存为有效的文件名,您应该格式化日期时间,以便它不会在您的文件名中产生无效字符。 (petelids)

使用.ToString扩展程序,您可以自动设置DateTime格式。

Stream stream = File.Open(date.ToString(MMddyyyy) + ".txt", FileMode.Open);

答案 1 :(得分:0)

您不需要使用Convert.ToString(args),您可以直接将日期转换为字符串。

Stream stream = File.Open(date.ToString()+".txt"); FileMode.Open);

如果您对字符串日期的格式有疑问,可以参考这个 Link

答案 2 :(得分:0)

您需要处理非法文件名字符,因此您应该将格式字符串传递给ToString()方法。例如:

Stream stream = File.Open(date.ToString("MMddyyyy HHmm") + ".txt", FileMode.Open);

有关DateTime.ToString()方法的更多信息,包括MSDN上的不同格式选项。

答案 3 :(得分:0)

您可以使用

之类的格式设置DateTime的格式
date.ToString("MM-dd-yyyy")

这应该确保文件名没有来自日期的任何非法字符。