Javascript Timestamp Conversion - javascript

the problem is as follows:
The file that i receive has a timestamp like
06/10/2016 02:58 AM
06/10/2016 05:20 PM
etc.
I'm using pentaho/kettle to eventually put this into a mysql database, the problem is that mysql only accepts yyyy-mm-dd hh-mm-ss timestamps. I'm not that great(if at all) in JavaScript and Kettle only support javascript, sadly.
I got it all split up, so it has a column called, 'days','months,'years', etc.
How can I make it so that if column Am/pm has "pm" in it, that it add 12 to the 'hours' column?
Or is there an easier way to do this in Kettle?
EDIT:
as i receive the csv i have 5 columns
1. Timestamp 2. Mobil_origin 3. mobile_Dest. 4.Call_cit 5.Call_dur
I want it so that all the timestamps get 'corrected' to the yyyy-mm-dd hh-mm-ss format.
so instead of 06/10/2016 05:20 PM it has to show 2016-10-06 17:20:00
It's the only thing that needs to happen actually, the rest of the file is good as is to be exported to mysql.

Related

How to print Firestore timestamp as formatted date and time like December 28,2020 at 3:52:04 AM UTC+5:30?

while fetching date from firestore I am getting Timestamp "seconds: 1608490949, nanoseconds: 275000000".I want to print it as properly formatted date and time. As it is "December 28,2020 at 3:52:04 AM UTC+5:30".
Below is my pic of code
obj.modify=this.dateconversion(obj.modify);
dateconversion( time:Timestamp){
return time.toDate();
}
It is returning me values "2020-12-27T22:22:04.000Z" but actual in firestore "December 28,2020 at
3:52:04 AM UTC+5:30".
Its seems giving me 5 hours back that's why one day back data is printing.
Can any one please suggest other way to do or where I am doing mistake.
You can call javascript date functions here because this converting into javascript date
dateconversion( time:Timestamp){
return time.toDate().toString();
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toString
Or for more control on formatting, you can use momentjs [https://momentjs.com/docs/]

How can I turn a UNIX timestamp into a Google Sheets date value?

I'm using Google Apps Script to retrieve some data through an API. The date that is given to me is a standard ISO 8601 string, and I need to convert that to a date number that will be understood by Google Sheets when I display it (i.e. as the Google Sheets / Excel standard date value)
Currently I'm using a workaround which is passing a string in my cells then converting it through the DATEVALUE() function but it is not optimal, since I cant use that function natively in my script (from my research on the topic).
I want to create a function directly in Google Apps script that converts the Javascript ol' Date() object into a readable value for my spreadsheet.
I've made a few attempts by converting the seconds since 1970 to the number of days since 1900 but I always have a two-hour gap that I can't explain. I suspect it has to do with the fact that I am in UTC+2, but can't really solve it, and adding two hours sounds like a bad fix.
The baseline of what I've gotten to so far is this :
function toDateNum(string) {
//Parsing to Unix Timestamp
date = new Date(string).getTime() / 1000
//Here I need convert to GSheets date value, add the number of days between 1900-01-01 and 1970-01-01
return date ;
}
I'm looking either for an arithmetic solve for this (converting and adding dates) or for a function integrated in the Google Sheets app that would allow me to work with this. Any suggestions ?
Thanks a lot !
EDIT (with additional clarification)
One of my issues is that the toISOString format returned is not supported by google sheets directly. It will only be read as a string, not as a date value. Currently, I'm working around this by sending the toISOString value in the cells and reformat using DateValue() directly in the table. I'm trying to do that natively in the script.
To the best of my knowledge, the date value supported by Google Sheets (and Excel) is the number of days since 1900-01-01. A simple arithmetic solution should be found (computing a unix timestamp from seconds then to days then add the difference between 1900-01-01 to 1970-01-01) but, then again, I get my two-hour gap that messes everything up.
You can achieve this by using new Date(string*1000), then passing the result to Utilities.formatDate(). Multiplying the Unix timestamp by 1000 means your argument is in milliseconds, rather than seconds. This Stackoverflow Question has a lot of good info for formatting dates using js.
function toDateNum(string) {
//convert unix timestamp to milliseconds rather than seconds
var d = new Date(string*1000);
//get timezone of spreadsheet
var tz = SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone();
//format date to readable format
var date = Utilities.formatDate(d, tz, 'dd-MM-yyyy hh:mm:ss a');
return date;
}
This script will use the timezone setting of your spreadsheet, but you can change this to use the time zone of the script or enter one yourself, examples below if you'd like to use these instead:
//use spreadsheet timezone setting
var tz = SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone();
//use script timezone setting
var tz = Session.getScriptTimeZone();
//enter your own time zone
var tz = 'GMT+0';
In the question linked below, the response by user79865 explains how to manage dates and their formats within sheets. The Sheets API documentation also specifies all the different formats you can use as well as how to create your own when working with dates and times. The Utilities.formatDate() will help you change the date format to whatever you need.
Question URL: https://webapps.stackexchange.com/questions/88621/basic-date-manipulation-in-google-script?newreg=7c66fdcf156f4ff5a30eb5fa4153b243
Sheets Documentation URL: https://developers.google.com/sheets/api/guides/formats

Casting from Timestamp/String to Date

I need to insert to database two variables:
- "start" with value "9:00"
- "end" with value "20:00"
It can be types String or Timestamp, i would prefer the string. But in database table the fields are "Time" type. So how to cast it? I got only time values without full date like month, days and years.
From what I have read, if you want to insert a string into a MySQL TIME column, it will have to have the format HH:mm:ss. So the easiest solution for you might be to obtain your start and end times in the format of 09:00:00 and 20:00:00.
I expect the following INSERT statement to proceed without error:
INSERT INTO yourTable (`time_from`, `time_to`)
VALUES
('09:00:00', '20:00:00')
To be clear, I would recommend putting in a little effort in your code to obtain time data in this format. Then just insert it into MySQL without further hassle.
In a Date Field you van only Store Dates without time Information.
So you Must change the fieldtype or add a nee Field for this

How to convert unix time parsed as MM/DD/YYYY to DD/MM/YYYY unix date

I've converted a whole heap of datestrings into unix timestamps (as milliseconds since epoch).
I used the moment.js date library which did a great job except there was one misunderstanding. In Australia, dates are usually represented in the format DD/MM/YYYY, however American dates are MM/DD/YYYY and moment seems to have assumed that the dates I parsed are in the American format.
This has lead to dates such as 01/10/2000 to be parsed as the tenth of January rather than the first of October. I've stored these dates as a number representing moments since epoch, so the date above has been stored as 970322400000 rather than 947426400000.
I want to convert all of the dates I've calculated into their proper values. I need a function which will take 970322400000 and convert it to 947426400000 for all dates that have been incorrectly calculated. Due to other circumstances I am unable to reprocess the dates from their original source.
Unfortunately - I have to run this in the mongodb shell.
Here is what I have already tried (the minified source of moment js at the top of the file).
db.reminders.find()
.map(function(v) {
return {
id: v._id,
original: moment(v.reminderDate).format('YYYY-MM-DD'),
repaired: moment(moment(moment(v.reminderDate).format('MM-DD-YYYY'), 'DD-MM-YYYY').valueOf()).format('YYYY-MM-DD')
};
})
However I'm afraid it's not working. In the outputted results I can't find any dates with a day component higher than 12.
What am I doing wrong here. How am I able to achieve this transformation?
This is actually an impossible problem. This is due to the fact that a value larger than 12 in the months part will flow over into the year.
'01-01-2012' -> x
'01-13-2011' -> x
'01-25-2010' -> x
There are infinity date-strings which can be evaluated for any given unix timestamp. Therefore you can't know what the original datestring was. This makes it impossible to change a timestamp value to the value that it would have been if it was parsed differently.
Fair warning with these sorts of problems, store the original datestring. If you need to have the date stored as a number for some reason it doesn't matter - store the original as well.

Moment.js 2 different date strings when i parse using moment gives same value

I am parsing 2 different date strings
var d1 = '2014-02-01T00:00:00.000+0530'
var d2 = '2014-02-23T00:00:00.000+0530'
when i parse them using moment
alert(moment(d1, 'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"').toDate());
alert(moment(d2, 'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"').toDate());
both of them print Sat Feb 1 2014 xxxxx
what is wrong with it??
here is the link to the fiddle i created
jsfiddle
I think your moment formatting string is causing you the problem. If I remove this, the dates do not print as the same.
http://jsfiddle.net/K5ub8/7/
EDIT: The specific issue is you are using dd for day, instead of DD. http://momentjs.com/docs/#/parsing/string-format/
Here is your fiddle fixed:
http://jsfiddle.net/K5ub8/9/
However, I am not 100% sure about the fractional seconds, I believe it is SSS instead of fffffff but I would test this if you need to cater for fractional seconds.
I should mention that if you are converting it back into a JavaScript date object anyway with toDate(), then you don't really need the moment formatting parameter as the date will be formatted in JSON Date format.
I would question why you would want to generate a moment formatted date, and then convert it back to JavaScript, a normal practice might be to receive a date in JavaScript format, then create a moment object which you can use to perform calculations and display in a nice user friendly way.
Simple answer: your format was off a bit.
http://jsfiddle.net/K5ub8/8/
After tweaking the format to be 'YYYY-MM-DDTHH:mm:ss.SSSZZ' rather than 'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"' it worked just fine. When you're trying to debug issues like this, it's always good to keep the format in a separate variable so you can use the same format that you're trying to parse out to display what you're getting. Had you done that, you would have noticed that 'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"' was messed up due to it printing out 2014-01-Fr"T"11:32:03.fffffff"-08:00". Which obviously isn't quite right.

Categories

Resources