powered by simpleCommunicator - 2.0.60     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / WinForms, .Net Framework [игнор отключен] [закрыт для гостей] / Как преобразовать строку "06/24/93 10:03AM" в дату?
6 сообщений из 6, страница 1 из 1
Как преобразовать строку "06/24/93 10:03AM" в дату?
    #32283892
V. Motchulsky
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
...
Рейтинг: 0 / 0
Как преобразовать строку "06/24/93 10:03AM" в дату?
    #32283992
Dronopotamus
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
смотри System.DateTime.Parse(...) или System.DateTime.ParseExact(...)
...
Рейтинг: 0 / 0
Как преобразовать строку "06/24/93 10:03AM" в дату?
    #32284002
V. Motchulsky
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Что-то у меня с Parse и ParseExact ничего не получается:

Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region  " Windows Form Designer generated code " 

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents ToParse As System.Windows.Forms.TextBox
    Friend WithEvents Res As System.Windows.Forms.TextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Button2 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.ToParse = New System.Windows.Forms.TextBox()
        Me.Res = New System.Windows.Forms.TextBox()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Button2 = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'ToParse
        '
        Me.ToParse.Location = New System.Drawing.Point( 224 ,  44 )
        Me.ToParse.Name =  "ToParse" 
        Me.ToParse.Size = New System.Drawing.Size( 148 ,  20 )
        Me.ToParse.TabIndex =  0 
        Me.ToParse.Text =  "06 / 24 / 93   10 :03AM"
        '
        'Res
        '
        Me.Res.Location = New System.Drawing.Point(224, 96)
        Me.Res.Name = "Res"
        Me.Res.Size = New System.Drawing.Size(148, 20)
        Me.Res.TabIndex = 1
        Me.Res.Text = ""
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point( 396 ,  44 )
        Me.Button1.Name =  "Button1" 
        Me.Button1.Size = New System.Drawing.Size( 60 ,  24 )
        Me.Button1.TabIndex =  2 
        Me.Button1.Text =  "Parse" 
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(136, 48)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(72, 16)
        Me.Label1.TabIndex = 3
        Me.Label1.Text = "Text to parse"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point( 136 ,  100 )
        Me.Label2.Name =  "Label2" 
        Me.Label2.Size = New System.Drawing.Size( 72 ,  16 )
        Me.Label2.TabIndex =  4 
        Me.Label2.Text =  "Parsed date" 
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(396, 96)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(60, 24)
        Me.Button2.TabIndex = 5
        Me.Button2.Text = "My parse"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size( 5 ,  13 )
        Me.ClientSize = New System.Drawing.Size( 596 ,  161 )
        Me.Controls.AddRange(New System.Windows.Forms.Control()  123 ;Me.Button2, Me.Label2, Me.Label1, Me.Button1, Me.Res, Me.ToParse 125 ;)
        Me.Name =  "Form1" 
        Me.Text =  "Form1" 
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim D As DateTime
        Try
            D.Parse(Me.ToParse.Text)
            Me.Res.Text = D.ToString()
        Catch
            Me.Res.Text =  "Parsing error" 
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim Hr As Integer
        Dim S As String
        Try
            S = Me.ToParse.Text
            Hr = CInt(Mid(S,  10 ,  2 ))
            If Mid(S,  15 ,  1 ) =  "P"  Then
                If Hr <  12  Then Hr +=  12 
            Else
                If Hr =  12  Then Hr =  0 
            End If
            Me.Res.Text = New DateTime(CInt( "20 " & Mid(S,  7 ,  2 )), CInt(Microsoft.VisualBasic.Left(S,  2 )), CInt(Mid(S,  4 ,  2 )), Hr, CInt(Mid(S,  13 ,  2 )),  0 )
        Catch
            Me.Res.Text =  "Parsing error" 
        End Try
    End Sub
End Class


и какую только строку не вводи - в лучшем случае 01/01/01 00:00. Пример с хелпа тоже не работает.
...
Рейтинг: 0 / 0
Как преобразовать строку "06/24/93 10:03AM" в дату?
    #32284057
Dronopotamus
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Да с дататаймами геморой ещё тот :)
Вот это работает:

Код: plaintext
1.
2.
3.
string date =  "06 / 24 / 93   10 :30AM";	
CultureInfo inf = new CultureInfo( "en-US" , true);			
string expectedFormats =  "MM/dd/yy hh:mmtt" ;
DateTime dtm = System.DateTime.ParseExact(date, expectedFormats, inf);
...
Рейтинг: 0 / 0
Как преобразовать строку "06/24/93 10:03AM" в дату?
    #32284109
V. Motchulsky
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
>Dronopotamus

Я что-то увлекся: завалил в форум по C# код на VB. Но должно же работать одинаково.
Делаю:
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim D As New DateTime()
        Dim inf As Globalization.CultureInfo = New Globalization.CultureInfo( "en-US" , True)
        Dim expectedFormats As String =  "MM/dd/yy hh:mmtt" 
        Try
            D.ParseExact(Me.ToParse.Text, expectedFormats, inf)
            Me.Res.Text = D.ToString()
        Catch
            Me.Res.Text =  "Parsing error" 
        End Try
    End Sub

Результат: "01.01.0001 0:00:00"!!!! :(
Бред какойто. Если на C# работает - значит на лицо первое отличие в языках... :)
...
Рейтинг: 0 / 0
Как преобразовать строку "06/24/93 10:03AM" в дату?
    #32294727
NoEtic
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Conbvrt.ToDateTime("06/24/93 10:03AM").ToShortDateString()
...
Рейтинг: 0 / 0
6 сообщений из 6, страница 1 из 1
Форумы / WinForms, .Net Framework [игнор отключен] [закрыт для гостей] / Как преобразовать строку "06/24/93 10:03AM" в дату?
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]