How do you convert a timestamp format within Javascript? - javascript

how do I convert something like
2020-12-01T15%3A48%3A39.862Z
to a timestamp format of
2018-09-15T15%3A53%3A00-07%3A00?
I am not very familiar with timestamp formats and manipulation of datetime in Javascript...

You can turn it into a DateTime directly, you just have to decode all that %3A stuff first:
var timeString = '2020-12-01T15%3A48%3A39.862Z';
var decodedTimestring = decodeURIComponent(timeString);
var date = new Date(decodedTimestring);
console.log(date.toLocaleString());

Related

How to convert a Timestamp into MySQL DateTime in JavaScript?

I have a timestamp format which is just like below which is an int.
1631514003973
I am passing this value to an API which is built with Node.JS. How can I convert this into Mysql DateTime format?
I checked this answer but it is about getting the current date and not converting a timestamp. I am coming from a Java background so this is bit confusing for me.
You can pass the Date object of JavaScript directly to MySQL. And MySQL will automatically generate the DateTime format from that Date object.
const date = new Date(1631514003973);
I assume that integer represents number in milliseconds since the Epoch time. If that is the case try this code:
const numberOfMs = 1631514003973;
const epochDate = new Date(1970,1,1);
const myDate = new Date(epochDate .getTime() + numberOfMs);
For that you can use either in nodejs layer. You need to convert the timestamp to date using below and use in the mysql query.
const date = new Date(1631514003973);
console.log(date)
For the use direct in mysql you can use mysql date function and
SELECT DATE_FORMAT(FROM_UNIXTIME(DATE(1631514003973)), '%e %b %Y') AS 'date_formatted' FROM table
we need to create a new function using JavaScript.
<script>
var timestamp = 1607110465663
var date = new Date(timestamp);
console.log("Date: "+date.getDate()+
"/"+(date.getMonth()+1)+
"/"+date.getFullYear()+
" "+date.getHours()+
":"+date.getMinutes()+
":"+date.getSeconds());
</script>
Output:
Date: 4/12/2020 19:34:25
If you want only date (MM/DD/YYYY), you should fallow this:
var timestamp=1370001284;
var todate=new Date(timestamp).getDate();
var tomonth=new Date(timestamp).getMonth()+1;
var toyear=new Date(timestamp).getFullYear();
var original_date=tomonth+'/'+todate+'/'+toyear;
console.log(original_date);

How to format dates with Pentaho Spoon

How do I convert the string 03-MAR-2021 to the string 20210303 with Javascript in Pentaho Spoon
start_date="03-MAR-21";
var new_startDate= new Date(start_date);
var date= moment(new_startDate).format('yyyyMMdd');
See common date formats
start_date="03-MAR-2021";
var date= str2date(start_date, "dd-MMM-yyyy");
var formatedDateString = date2str(date, "yyyyMMdd");
I get the date in a string like '03-Mar-2021' but I need to convert to string '20210303' (YYYYMMDD) I can make in JScript or directly on Query. But I have problems when I try to

Javascript: How to convert exif date time data to timestamp? [duplicate]

This question already has answers here:
javascript: how to parse a date string
(4 answers)
Closed 5 years ago.
In javascript, while using exif-js to extract metadata of an image file, I am getting date time format as 2017:03:09 14:49:21.
The value in the DateTimeOriginal property is formatted as YYYY:MMY:DD HH:MM:SS. When I use var d = new Date(2017:03:09 14:49:21), it returns NaN. It's the colons in between the YYYY, MM, and DD which causes problem.
How to solve this problem?
Thanks in advance.
Don't use the built-in parser (i.e. Date constructor or Date.parse) for parsing strings as it's largely implementation dependent and unreliable. If you can trust the date to be valid, then the following will do:
/* Parse date string in YYYY-MM-DD hh:mm:ss format
** separator can be any non-digit character
** e.g. 2017:03:09 14:49:21
*/
function parseDate(s) {
var b = s.split(/\D/);
return new Date(b[0],b[1]-1,b[2],b[3],b[4],b[5]);
}
console.log(parseDate('2017:03:09 14:49:21').toString());
It's fairly easy to add validation to the values. Otherwise, use a library and make sure you specify the format to parse.
My recommendation would be to use Moment (http://momentjs.com/docs/), as it provides clean parsing of dates. With Moment, what you want is this:
var tstamp = moment("2017:03:09 14:49:21", "YYYY:MM:DD HH:mm:ss");
var date = tstamp.toDate();
You can do simple string manipulation and create date if the format is always the same, as:
var str = "2017:03:09 14:49:21".split(" ");
//get date part and replace ':' with '-'
var dateStr = str[0].replace(/:/g, "-");
//concat the strings (date and time part)
var properDateStr = dateStr + " " + str[1];
//pass to Date
var date = new Date(properDateStr);
console.log(date);

How can I extract date and time from php variable in javascript?

I have a quastion.
So I get from server a variable with date-time string which looks like this: '31/08/2015 13:24'.
How can extract from this string separately date and time?
You could split the string:
var dateTime = '31/08/2015 13:24'.split(" ");
console.log(dateTime[0]); //date
console.log(dateTime[1]); //time
Or use js Date object to get the day, month, year, hours, etc:
var dateTime = new Date('31/08/2015 13:24');

convert string to Default date Format

Hi I have use the string object for represent the date in the following format
var date="18/01/2011";
var dateFormat="dd/MM/yyyy";
Note:
In my scenorio i have use the dd/MM/yyyy format;
dateFormat will be different in my client side.
how to convert these date default JavaScript dateFormat as MM/dd/yyyy in Generic way.
I have tried in by split date by and swap the month ,date to achieve this requirement. But in my client side i dont know about the format of the date how to convert any other format to default Javascript format
Hope this helps:
var date="18/01/2011";
var parts = date.split('/');
var result = new Date(parts[2], parts[1], parts[0]);
var datestring = "2014-02-27:04:05";var d = Date(datestring);

Categories

Resources