Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Long date to Short
#1
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.
#2
No.
DateTime.FromStr cannot parse long date string. And I don't know API that can.
#3
It seems C# DateTime.ParseExact can parse it. In QM you can use C#.
https://stackoverflow.com/questions/2828...-date-time
#4
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

Code:
Copy      Help
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)
#5
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.
#6
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

Code:
Copy      Help
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}")
#7
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
Code:
Copy      Help
;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 http://www.quickmacros.com/forum/showthread.php?tid=679

Function Macro_180320b
Code:
Copy      Help
;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
Code:
Copy      Help
str s="23/03/18"
DATE d1(s)
out d1

Best regards.
#8
this code works on my pc every time.

Code:
Copy      Help
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("," 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..
#9
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
Code:
Copy      Help
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
#10
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
Code:
Copy      Help
;/
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
#11
Dear Kevin,

Thank you very much indeed. Your code is perfect!

Best personal regards
Simos


Forum Jump:


Users browsing this thread: 1 Guest(s)