How To Convert String To Date

Table of contents:

How To Convert String To Date
How To Convert String To Date

Video: How To Convert String To Date

Video: How To Convert String To Date
Video: How to convert String to Date in java? 2024, May
Anonim

Converting a string to a date is one of the most common operations a programmer encounters when writing certain programs or scripts. Each language implements this function in its own way and has its own tools for processing the string data type.

How to convert string to date
How to convert string to date

Instructions

Step 1

The Delphi programming language uses the StrToDate () function to convert a string to a date, and the string must be in the "number number number" format. The DateToStr () function is responsible for the reverse conversion. If you need to convert the date of the format "01 January 2000", then first you have to convert the value of the month to a number, and then carry out the output using the appropriate function.

Step 2

C # also uses a corresponding function. For example, if the date is in the format "Sat, 01 Jan 2000", then you can use the Convert. ToDate () or Date. Parse () function.

Step 3

PHP has a special function strtotime (). For example, the query "echo strtotime (“01 January 2000”);" will convert the specified string to a date and display it on the screen. If you need to translate a string like "01012001" into the correct date format, then it is best to use regular expressions:

function string_and_time ($ time) {

return preg_replace (“/ (d {2}) (d {2}) (d {4}) / e”,”\ '. match_month (' / 2 ').' / 3”, $ time); }

echo string_and_time (01012001);

Step 4

For C ++, there is a sscanf () function that does the appropriate conversions. If you use the Qt4 library, you can use the function "QDate:: fromString (“01.01.2001”,“dd. MM.yyyy”)".

Step 5

For Pascal, the StrToDate () function easily handles converting strings to a date, but if your program uses the names of months, you will have to use VarToDateTime ():

var

DateOne, DateTwo, DateThree: TDateTime;

Begin

DateOne: = VarToDateTime (‘January 1, 2000’);

ShowMessage (DateToStri (DateOne));

end;

Step 6

In Java, you can use the following script to convert:

Java.lang. Integer:

String myString = “1”;

Int my = Integer.parseInt (myString);

Java.text. DateFormat:

DateFormat formDate = DateForman.getDateInstance ();

Java.util. Date:

Date ourDate = dateFormat.parse (“01.01.2000”);

Recommended: