Skip to content

Commit

Permalink
Fix problem create date in getNextTime
Browse files Browse the repository at this point in the history
  • Loading branch information
Tutitoos committed Jul 23, 2023
1 parent 44079dc commit b51c499
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "util-tiempo",
"version": "1.0.51",
"version": "1.0.52",
"description": "util-tiempo para calcular tiempos, para obtener la fecha a elegir, con muchas opciones!",
"author": "Tutitoos",
"license": "MIT",
Expand Down
6 changes: 5 additions & 1 deletion src/lib/getNextTime.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Validate from "../Validate";
import { type GetNextTimeProps } from "../types";
import { formatDateFromString } from "../util";
import getMs from "./getMs";
import getTime from "./getTime";

Expand All @@ -21,8 +22,11 @@ const getNextTime = (...options: Partial<GetNextTimeProps>): number => {
throw new Error("Invalid time format, must be HH:MM:SS");
}

// Get the current date in the specified timezone
const date = formatDateFromString(new Date().toLocaleString(local, { timeZone: timezone }));

// Get the current timestamp in the specified timezone
const timestamp = new Date(new Date().toLocaleString(local, { timeZone: timezone })).getTime();
const timestamp = new Date(date).getTime();

// Split the time into hour, minute, and second components
const [hour, minute, second] = getTime({
Expand Down
30 changes: 30 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,33 @@ export const compareDataParse = (unit: number, format: DateFormats, options: Dat
// Return formatted string with unit and last alias
return `${unitParsed} ${aliases.at(-1) as string}`;
};

/**
* Parses a string representing a date and returns a Date object.
*
* @param dateString - The string representation of the date in the format "dd/mm/yyyy, hh:mm:ss".
* @returns The parsed Date object.
*/
export const formatDateFromString = (dateString: string) => {
// Split the input string into the date and time components
const [fecha, tiempo] = dateString.split(", ");

// Split the date component into day, month, and year
const [day, month, year] = fecha.split("/");

// Split the time component into hour, minute, and second
const [hour, minute, second] = tiempo.split(":");

// Parse the components into numbers
const dayNumber = parseInt(day, 10);
const monthNumber = parseInt(month, 10) - 1;
const yearNumber = parseInt(year, 10);
const hourNumber = parseInt(hour, 10);
const minuteNumber = parseInt(minute, 10);
const secondNumber = parseInt(second, 10);

// Create a Date object using the parsed components
const dateObject = new Date(yearNumber, monthNumber, dayNumber, hourNumber, minuteNumber, secondNumber);

return dateObject;
};

0 comments on commit b51c499

Please sign in to comment.