Posts: 1,058
Threads: 367
Joined: Oct 2007
I understand that "s.timeformat("{DD}" d)" converts DATE'd to a long format string. I wonder whether there exists a simple way to convert s, as generated from above statement back to a DATE'd variable. I am sorry if I am asking something I should know. Many thanks in advance.
Posts: 12,071
Threads: 140
Joined: Dec 2002
03-08-2018, 12:06 PM
(This post was last modified: 03-08-2018, 12:07 PM by Gintaras.)
No.
DateTime.FromStr cannot parse long date string. And I don't know API that can.
Posts: 12,071
Threads: 140
Joined: Dec 2002
It seems C# DateTime.ParseExact can parse it. In QM you can use C#.
https://stackoverflow.com/questions/2828...-date-time
Posts: 1,336
Threads: 61
Joined: Jul 2006
03-20-2018, 06:56 PM
(This post was last modified: 03-20-2018, 07:30 PM by Kevin.)
simplest way to convert timeformat string back to Date format in this case is remove the day and first comma and then can be converted back to Date
str s.timeformat("{DD}")
out s
// remove day and comma from string
int i = find(s "day," 0 1)
s.remove(0 i+4)
s.trim
out s
//convert to Date
DATE d1(s)
out d1
//convert to DateTime if needed
DateTime d.FromStr(s) ;;;directly from string
out d.ToStr(1)
DateTime d2.FromDATE(d1);;from Date
out d2.ToStr(1)
Posts: 1,058
Threads: 367
Joined: Oct 2007
03-20-2018, 07:57 PM
(This post was last modified: 03-20-2018, 08:49 PM by ssimop.)
Thank you. I will try it and let you know. Best regards.
I understand that :
str s="20-03-2006"
DATE dt(s)
works perfectly.
However, I wonder whether :
str s="March 20, 2018"
DATE dt(s)
it works too.
In my case I get a "Type mismatch" error. I would welcome your advice.
Posts: 1,336
Threads: 61
Joined: Jul 2006
03-22-2018, 04:48 AM
(This post was last modified: 03-22-2018, 04:49 AM by Kevin.)
If i am not mistaken date strings are locale specific . if your long date string is not in english will have to adjust find .
if the day is first and in another language change code to something like this
str s.timeformat("{DD}")
out s
// remove day and comma from string
int i = find(s "," 0 1)
s.remove(0 i+1)
s.trim
out s
//convert to Date
DATE d1(s)
out d1
//convert to DateTime if needed
DateTime d.FromStr(s) ;;;directly from string
out d.ToStr(1)
DateTime d2.FromDATE(d1);;from Date
out d2.ToStr(1)
this finds the first comma and removes everything to the left of it. to help more post the output of str s.timeformat("{DD}")
Posts: 1,058
Threads: 367
Joined: Oct 2007
Thank you for your patience with me. I have converted your macro in english locale. It still gives an error at the date conversion statement:
Function Macro_180320a
;www.quickmacros.com/forum/showthread.php?tid=6379
;str s.timeformat("{DD}")
str s.timeformat("{DD}" 0 WINAPI.LANG_ENGLISH|WINAPI.SUBLANG_ENGLISH_US)
out s
// remove day and comma from string
int i = find(s "," 0 1)
s.remove(0 i+1)
s.trim
out s
//convert to Date
DATE d1(s)
;Error (RT) in <open ":7772: /278">Macro_180320a: 0x80020005, Type mismatch. <help #IDP_ERR>?
out d1
//convert to DateTime if needed
DateTime d.FromStr(s) ;;;directly from string
out d.ToStr(1)
DateTime d2.FromDATE(d1);;from Date
out d2.ToStr(1)
Let us put it in a simpler way. The following simple macro does not work and the explanation is give in thread www.quickmacros.com/forum/showthread.php?tid=679
Function Macro_180320b
;www.quickmacros.com/forum/showthread.php?tid=679
str s="March 23, 2018"
DATE d1(s)
Nevertheless, the following one it works:
Function Macro_180320c
str s="23/03/18"
DATE d1(s)
out d1
Best regards.
Posts: 1,336
Threads: 61
Joined: Jul 2006
this code works on my pc every time.
str s="March 23, 2018"
DATE d1(s)
Outputs
03/22/2018
this code
str s.timeformat("{DD}")
out s
// remove day and comma from string
int i = find(s "," 0 1)
s.remove(0 i+1)
s.trim
out s
//convert to Date
DATE d1(s)
out d1
//convert to DateTime if needed
DateTime d.FromStr(s) ;;;directly from string
out d.ToStr(1)
DateTime d2.FromDATE(d1);;from Date
out d2.ToStr(1)
Outputs
Thursday, March 22, 2018
March 22, 2018
03/22/2018
03/22/2018
03/22/2018
if your output is not the same then its a language issue..
Posts: 1,336
Threads: 61
Joined: Jul 2006
03-23-2018, 06:16 AM
(This post was last modified: 03-23-2018, 06:22 AM by Kevin.)
i think your best bet is to try what Gintaras said and use C#
here is some basic C# DateTime examples in qm
Function DateTimeExamples
out
str code=
;using System;
;using System.Globalization;
,,,,,;
;public class Example
;{
,;public static void Main()
,;{
,,;string res = DateTime.ParseExact("Friday, March 23, 2018 12:05:33 AM", "dddd, MMMM d, yyyy hh:mm:ss tt", CultureInfo.InvariantCulture).ToShortDateString();
,,;Console.WriteLine(res);
,,;string a = DateTime.Now.ToShortTimeString();
,,;string b = DateTime.Now.ToLongTimeString();
,,;string c = DateTime.Now.ToLongDateString();
,,;DateTime d = DateTime.Today;
,,;Console.WriteLine(a);
,,;Console.WriteLine(b);
,,;Console.WriteLine(c);
,,;Console.WriteLine(d.ToString());
,,;DateTime now = DateTime.Now;
,,;string longdate = now.ToString("D");
,,;string shortdate = now.ToString("d");
,,;string longtime = now.ToString("T");
,,;string shorttime = now.ToString("t");
,,;Console.WriteLine(longdate);
,,;Console.WriteLine(shortdate);
,,;Console.WriteLine(longtime);
,,;Console.WriteLine(shorttime);
,,;Console.WriteLine(DateTime.Now.ToLongTimeString().ToString());
;;;;;;;;;Console.WriteLine(DateTime.Now.ToShortTimeString().ToString());
,;}
;}
CsExec code "called Main"
there are alot more ways start reading here standard-date-and-time-format-strings
Posts: 1,336
Threads: 61
Joined: Jul 2006
03-26-2018, 06:53 AM
(This post was last modified: 03-26-2018, 07:11 AM by Kevin.)
This function should do what you need . C# function to convert long date string to short date instructions and example how to use in function help comments
Function LongDateToShortDateCslocaleSpecific
;/
function ~&results ~date ~pattern ~CultureCode
;converts longdate string to shortdate string in c#
;REMARKS
;can be easily converted for other languages
;just change CulturCode "el-GR" in calling function to language needed
;and adjust the datepattern string "dddd, d MMMM yyyy" as well.
;see example below.
;to match the culture date string your trying convert
;for culture codes and date formats see
;http://www.basicdatepicker.com/samples/cultureinfo.aspx
;
;EXAMPLE
;for greek longdate to shortdate
;out
;str longdate = "Κυριακή, 25 Μαρτίου 2018"
;str result
;str datepattern = "dddd, d MMMM yyyy"
;str locale = "el-GR"
;LongDateToShortDateCslocaleSpecific(result longdate datepattern locale)
;out result
str code=
;using System;
;using System.Globalization;
;public class Example
;{
;,,public static string StaticFunc(string date, string pattern, string culturecode)
;,,{
;,,,;CultureInfo provider = String.IsNullOrEmpty(culturecode)
;;,,,,;? CultureInfo.InvariantCulture // Or use other default
;;,,,,;: new CultureInfo(culturecode);
;,,,string res = DateTime.ParseExact(date, pattern, provider).ToShortDateString();
;,,,return res;
;,,}
;}
str R=CsFunc(code date pattern CultureCode)
results = R
ret
Posts: 1,058
Threads: 367
Joined: Oct 2007
Dear Kevin,
Thank you very much indeed. Your code is perfect!
Best personal regards
Simos
|