JS using OR in string split [duplicate] - javascript

This question already has answers here:
How do I split a string with multiple separators in JavaScript?
(25 answers)
Closed 2 years ago.
I have CSV with dates in format DD-MM-YYYY but sometimes I work in Excel and it changes the dates to format DD/MM/YYYY
Code example
var dateParts = value['TAX_CALCULATION_DATE'].split("/"); //id date has "-" or "/"
var dateObject = new Date(+dateParts[2], dateParts[1] - 1, +dateParts[0]);
firestoreObject.TAX_CALCULATION_DATE = firebase.firestore.Timestamp.fromDate(dateObject);
How to include "/" and "-" in one split?

You can use regex in split;
// Matches / or -
const regex = /\/|-/g;
yourDate.split(regex);

Related

How can I format string type of time/date in React.js [duplicate]

This question already has answers here:
Parsing ISO 8601 date in Javascript
(5 answers)
Closed last year.
Time format i have is 2022-02-16T12:33:44Z
How can is make this
2022-02-16
12:33
including br tag.
what i tried was date.split("T") but don't know what to do next
const str = "2022-02-16T12:33:44Z"
str.split("T")[0]
str.split("T")[1].slice(0,-4)
Output
2022-02-16
12:33

Reformat date string [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 4 years ago.
Given a date string in yyyy-MM-dd format like 2018-08-09, how would I convert it to yyyy/MM/dd format (2018/08/09) and dd/MM/yyyy format (09/08/2018) in javascript?
I have seen How to format a JavaScript date but it is about how to format a javascript date while i want to format a javascript date string
To replace all the hyphens in the date String with slashes, you can split the String on "-" and join it on "/".
var dateStr = "2018-08-09";
var newDateStr = dateStr.split('-').join('/');//output is 2018/08/09
console.log(newDateStr);
To change a date String from yyyy-MM-dd format to dd/MM/yyyy format, you can split the String on "-", reverse it, and join it on "/".
var dateStr = "2018-08-09";
var newDateStr = dateStr.split('-').reverse().join('/');//output is 09/08/2018
console.log(newDateStr);

String to Date conversion using JavaScript [duplicate]

This question already has answers here:
Parsing a string to a date in JavaScript
(35 answers)
Closed 4 years ago.
I have string value '05-Jan-18' to be converted to date format using javascript. can someone help me on this.
You can just do:
var d = new Date("05-Jan-18")
Yeah '05-Jan-18' does not work everywhere. However
new Date("05-Jan-18".replace(new RegExp('-', 'g'), ' '))
should work

Get Current Date/Time with JavaScript [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 6 years ago.
I need to get the current date and time javascript in the following format
YYYY-MM-DD HH:MM:SS
But cannot work out how to do it.
var d = new Date();
timesheetGrid.cellById(rId,15).setValue(d);
This is where I am using the data.
Any help would be greatly appreciated.
How about simply:
d.toISOString().replace('T', ' ').replace(/\..*$/, '');
Take the ISO string, replace the "T" with a space, and trim off the milliseconds.

How convert String to Date in Javascript? [duplicate]

This question already has answers here:
Convert dd-mm-yyyy string to date
(15 answers)
how to parse a "dd/mm/yyyy" or "dd-mm-yyyy" or "dd-mmm-yyyy" formatted date string using JavaScript or jQuery [duplicate]
(5 answers)
Closed 8 years ago.
I have string as 06/08/2013 that I want to convert into Date Object
I do
var transactionDate = Date.parse(transactionDateAsString);
and I get NaN
How can I tell javascript to format the string as dd/mm/yyyy?
Break it down and spoon-feed it:
var parts = transactionDateAsString.split("/");
var date = new Date(parts[2],parts[1]-1,parts[0]);
Date.parse() from the docs:
Parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.
You probably want to use the Date constructor:
var transactionDate = new Date(transactionDateAsString);

Categories