3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Csharp > datetime > DateTime型とstring型の変換

Last updated at Posted at 2017-11-13
動作環境
Windows 8.1 Pro (64bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2

System.DateTime型関連の変換。

string型からDateTime型へ変換

参考: unity > time > string型の日時をSystem.DateTime型に変換する > private System.DateTime curDt = System.DateTime.Parse("2015/09/08 12:30:45");

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)。
qiita.png

[from datetime]ボタンを押した。
qiita.png

link

検索用キーワード

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?