動作環境
Windows 8.1 Pro (64bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2
System.DateTime型関連の変換。
string型からDateTime型へ変換
TryParse()やParse()を使う。
private void B_toDateTime_Click(object sender, RoutedEventArgs e)
{
System.DateTime wrkdt;
string strtime = T_datetime.Text;
if (System.DateTime.TryParse(strtime, out wrkdt) == false)
{
Console.WriteLine("Error: invalid datetime format [" + strtime + "]");
return;
}
Console.WriteLine(wrkdt.ToLongTimeString());
}
DateTime型からstring型への変換
参考: Unity 日付・時刻の取得とDateTimeのToString 1 by さわやかレモンティーさん
情報感謝です。
DateTime型構造体を利用する例と、上記のリンクの例を使った例は以下。
private void b_fromDateTime_Click(object sender, RoutedEventArgs e)
{
System.DateTime wrkdt = System.DateTime.Now;
string infmsg1 = string.Format("{0:00}:{1:00}:{2:00}", wrkdt.Hour, wrkdt.Minute, wrkdt.Second);
Console.WriteLine(infmsg1);
string infmsg2 = wrkdt.ToString("HH:mm:ss");
Console.WriteLine(infmsg2);
}
別途、C#のあまり見かけない便利な5つの記法の「1. 文字列挿入」の方法も良さそうです。
code
上記を実装したコード。
MainWindow.xaml
<Window x:Class="_171113_t1920_datetimeEncodeDecode.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:_171113_t1920_datetimeEncodeDecode"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<TextBox Width="100" Height="30" Name="T_datetime">12:30:45</TextBox>
<Button Name="B_toDateTime" Width="100" Height="30" Content="to datetime" Click="B_toDateTime_Click"/>
<Button Name="b_fromDateTime" Width="100" Height="30" Content="from datetime" Click="b_fromDateTime_Click"/>
</StackPanel>
</Grid>
</Window>
MainWindow.xaml.cs
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;
namespace _171113_t1920_datetimeEncodeDecode
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void B_toDateTime_Click(object sender, RoutedEventArgs e)
{
System.DateTime wrkdt;
string strtime = T_datetime.Text;
if (System.DateTime.TryParse(strtime, out wrkdt) == false)
{
Console.WriteLine("Error: invalid datetime format [" + strtime + "]");
return;
}
Console.WriteLine(wrkdt.ToLongTimeString());
}
private void b_fromDateTime_Click(object sender, RoutedEventArgs e)
{
System.DateTime wrkdt = System.DateTime.Now;
string infmsg1 = string.Format("{0:00}:{1:00}:{2:00}", wrkdt.Hour, wrkdt.Minute, wrkdt.Second);
Console.WriteLine(infmsg1);
string infmsg2 = wrkdt.ToString("HH:mm:ss");
Console.WriteLine(infmsg2);
}
}
}
動作例
[to datetime]ボタンを押した(テキスト: 12:30:45)。
[to datetime]ボタンを押した(テキスト: 12:30:45)。
[to datetime]ボタンを押した(テキスト: 12:30:45xxx)。
link
検索用キーワード
- FormatDateTime
- WPF