Convert datetime zone string to datetime javascript - javascript

I try to convert the format: 2017-03-07T17:26:15-03:00 to a format like
yyyy/MM/dd HH:mm:ss for i can use DateTime in MySQL DataBase. I receveid those values from a XML file.
I find a lot of options here, but not working, i'm using PDI from Pentaho to convert the XML and save in a Database.
Use JavaScript to convert a date string with timezone to a date object in local time
Thanks so much.

You can extract individual date parts and combine them to get the desired string. Something like this:
var d = new Date('2017-03-07T17:26:00-03:00');
var result = d.getFullYear()+
'/'+('0' + (d.getMonth()+1)).slice(-2)+
'/'+('0' + d.getDate()).slice(-2)+
' '+('0' + d.getHours()).slice(-2)+
':'+('0' + d.getMinutes()).slice(-2)+
':'+('0' + d.getSeconds()).slice(-2);
console.log(result);

You cant use the calculator step, create a copy of field A, wich is the date that you want to change, and select the format date that you want, sorry for my english, is rusty, regards

Related

Working with date formats in Java and MySQL

Ok this is really bugging me.
I am developing a web app and I need to work with dates. When a date is displayed in a view, or whenever a date is entered into a form I need the format to be dd/mm/yyyy.
What data type do I choose for my SQL database columns which contain dates. 'Date' doesn't seem to work, do I use varchar?
But If I use varchar how do I use java script to perform arithmetic with dates.
Do I do some conversions server-side?
Please advise the best practices.
Also Im using laravel if theres any useful stuff already built in.
Date is the correct type to use in SQL DB.
To access the value use ISO date format "YYYY-MM-DD HH:mm:ss" .
You can create it from Java Date Object by using toISOString() method.
For easier time format conversion I can also recommend to check out Moment.js.
Best practice to save the date in MySQL table as date field only. Which saves the date string in YYYY-MM-DD HH:mm:ss format.
You need to make sure the following things.
Before inserting date into MySQL change the format of date string to YYYY-MM-DD HH:mm:ss.
When you retrieve the date from database convert the date string from YYYY-MM-DD HH:mm:ss to your desired format.
You can use SimpleDateFormat class in Java to convert the dates
format. Use format() function to format the date in desired and
parse() function to get the Java date object from string.
Reference: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
You should use varchar2 in mysql.
You can retrieve that varchar type date in javascript and create date object.
var d=dbdate;
var date = new Date(d);
And you can perform all javascript functions on date.
Hope it will help.
Javascript Date() object supports separate entries of date by:
var date=new Date();
var y=date.getFullYear();//4 digits
var m=date.getMonth()+1;//0-11 digits, plus 1 for true state
var d=date.getDate();//1-31
var dateSQL=y+'-'+m+'-'+d;//i.e 2016-07-25
you can use MySQL filed as datetime and also insert datetime format but when you will show then process it as you want as like
when you insert value in table then you can also format it as like
<?php $mysqltime = date ("Y-m-d H:i:s", $phptime); ?>
where $phptime is your input variable
$str = suppose $row['date'] (mysql filed value)
date("d/m/Y", strtotime($str));
where $str your retrieve date filed

Timestamp in node js different from database

In database time-stamp is saved as 2015-12-05 10:53:12 but when I fetch record using mysql query it return this time-stamp like 2015-12-05T10:53:12.000Z.
Why? any issue?? please help.
In your database the timestamp is saved in binary numerical format. "2015-12-05 10:53:12" & "2015-12-05T10:53:12.000Z" are string representation of that value that you see in some application.
It's the same value and there's no issue to solve here.
To convert your Date-Object (which is represented as ISOString with "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") you have to parse it after reading from the db.
In JavaScript you can do that by simply passing the string in the constructor, e.g. var myDate = new Date("2015-12-05T10:53:12.000Z")
With a custom format you could do it like this for example:
var dateString = "2015-12-05 10:53:12";
var date = Date.parse(dateString, "yyyy-MM-dd HH:mm:ss");
In order to represent it as "Dec 16, 2015" you have to parse it afterwards:
There are some good libraries for doing this (e.g. momentjs or with angularjs - date filters)
Without that you have do manually do it somehow like this (where you pass your date object created before):
function parseDate(date) {
var months = ['JAN','FEB','MAR','APR','MAY','JUN',
'JUL','AUG','SEP','OCT','NOV','DEC'];
return months[date.getMonth()]+" "+("0" + date.getDate()).slice(-2)+", "+date.getUTCFullYear();
}
You can use MySQL's DATE_FORMAT, eg.
SELECT DATE_FORMAT('2009-10-04 22:23:00', '%W %M %Y'); -> 'Sunday October 2009'
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format

Force data to a specific culture with 'toLocaleDateString'

I have got a date and I like to convert it 'toLocaleDateString' but in the language I like. I am trying to do it, but it always give me the format of the user language(spanish).
var date = new Date();//or any date
console.log("date: " + date.toLocaleDateString("en-GB");
Thanks!

Manipulating Json string showing dateTime

Let me show you my code before I ask you my question,For your Info : meetings_String.TABLE[0].ROW[(j)].COL[5].DATA is dateTime object in sql server having format dd/mm/yyyy AM hh:mm:ss.....my code is
meetingsSDate=meetings_String.TABLE[0].ROW[(j)].COL[5].DATA; //meetingsSdate now has data in the format of 'dd/mm/yyyy AM hh:mm:ss'
Meet_startTime=meetingsSDate.replace(/\d\d\/\d\d\/\d\d\d\d /i,'');// Meet_startTime now has data in the format of 'AM hh:mm:ss'
I want to make another string using Meet_startTime(or otherwise) in javascript which will be of the format like HH:MM am or pm(24hr clock would be still better)
P.S:I hope you understand my question
It would be preferable to parse your values into actual Date objects, rather than just manipulating strings in different manners. From a Date object, you can extract the date and time parts and combine them in any order you want.
You could try...
meetingsSDate = meetingsSDate.split(' ');
Meet_startTime = meetingsSDate[2] + ' ' + meetingsSDate[1];

Changing the format of a date in Yahoo UI

I'm using calendar from Yahoo UI as follows:
calDate = (calDate.getMonth() + 1) +
'/' + calDate.getDate() + '/' +
calDate.getFullYear();
This displays a MMDDYYYY format.
I want to change this format to YYYY-MM-DD so that I can insert it into a MySQL database.
I changed the code to:
calDate = calDate.getFullYear() + '-'
+ (calDate.getMonth() + 1) + '-' + calDate.getDate();
It worked but the problem is now that when I want to change the selected date the calender doesn't display the date and instead shows NAN.
It's typically best to pass the date to your back end as a unix timestamp and convert it in the database itself. In the code where you construct your POST data for the server, you'd pass something like this:
var t = calDate.getTime()/1000;
Note the divide by 1000. This is required because javascript timestamps are in milliseconds, while MySQL requires seconds.
In your SQL statement, you'll pass the timestamp as is, and use the FROM_UNIXTIME function to convert it to your required format:
INSERT INTO ... VALUES ( FROM_UNIXTIME($t), ...)
Naturally there will be some code in between that converts t from javascript into $t in your back end script, and then passes that on to the SQL.
Now, if you really need to format dates on the front end, you can make use of the handy YAHOO.util.Date utility. Just do something like this:
alert(YAHOO.util.Date.format(calDate, {format: "%Y-%m-%d" }));
Much easier than calling getFullYear, getMonth, and getDate
bluesmoon has also written two excellent articles on YUIBlog on the subject of date formatting:
http://yuiblog.com/blog/2009/02/11/date-formatting-pt1-2/
http://yuiblog.com/blog/2009/02/25/date-formatting-pt2/
-Eric
You should change the format of the date just before it is inserted on the server side, let the client side have the format that it wants, and change the date around later.
cal1.cfg.setProperty("DATE_FIELD_DELIMITER", "-");
cal1.cfg.setProperty("MDY_YEAR_POSITION", 1);
cal1.cfg.setProperty("MDY_MONTH_POSITION", 2);
cal1.cfg.setProperty("MDY_DAY_POSITION", 3);

Categories