How to fix date format?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Apr 25 21:08:11 PDT 2017


On 04/25/2017 10:41 AM, Suliman wrote:
> I am using mysql native. Date in DB have next format: 2016-11-01 06:19:37
>
> But every tile when I am trying to get it I am getting such format:
> 2016-Oct-31 15:37:24
>
> I use next code:
> writeln(point[1].coerce!string);
>
> Why coerce is forcing format changing? How I can extract result as
> without month name between digits or easily convert it in proper data
> format?

Here's a silly little code that gets the job done. :)

import std.stdio;

string[string] numeralMonth;

static this() {
     numeralMonth = [ "Jan" : "01", "Feb" : "02", "Mar" : "03", "Apr" : 
"04",
                      "May" : "05", "Jun" : "06", "Jul" : "07", "Aug" : 
"08",
                      "Sep" : "09", "Oct" : "10", "Nov" : "11", "Dec" : 
"12" ];
}

auto goodDateFormat(string str) {
     import std.range : chain, choose;
     if (str.length >= 8) {
         auto key = str[5..8];
         auto numeral = key in numeralMonth;
         if (numeral) {
             import std.string : format;
             return format("%s%s%s", str[0..5], *numeral, str[8..$]);
         }
     }
     return str;
}

void main() {
     writeln(goodDateFormat("2016-Oct-31 15:37:24"));
}

Ali



More information about the Digitalmars-d-learn mailing list