我的Parse应用程序中的表中有一列,当它应该是一个日期时被错误地定义为字符串。不幸的是,它已经填充了许多行。例如,字符串数据的格式为:
11 05 18 14 04 PDT
(我相信那将是:yy MM dd hh mm z)
我想运行一个Cloud Code作业,它从每一行读取字符串数据并将实际日期存储在另一列中(假设这个新列存在)。转换完成后,我会手动删除原始的,错误的字符串列。
以下是我目前的工作,现在正在工作,但导入的时区在转换中丢失(从PDT到GMT).moment.js
Parse.Cloud.job("publicationDateMigration", function(request, status) {
// Set up to modify user data
var moment = require('moment');
Parse.Cloud.useMasterKey();
var counter = 0;
// Query for all episodes
var PSEpisode = Parse.Object.extend("PSEpisode");
var query = new Parse.Query(PSEpisode);
query.doesNotExist("publicationDate")
query.limit(10);
query.find({
success: function(results) {
console.log("preparing for loop-");
var i=0;
while (results[i]){
var convertedPubDate = moment(results[i].get('pubDate'), "YY MM DD HH mm ss Z").toDate();
console.log(“unconverted date is " + results[i].get('pubDate'));
console.log("converted date is " + convertedPubDate);
results[i].set('publicationDate', convertedPubDate);
i++;
}
Parse.Object.saveAll(results);
status.success("Date migration completed successfully.");
},
error: function(error) {
status.error("Uh oh, something went wrong with date conversion.");
}
});
});
答案 0 :(得分:0)
根据Moment Docs on String + Format" Z"或" ZZ"占位符适用于:时区偏移为+07:00
或+0700
。
它不了解" PDT",因此您需要先进行替换以将其转换为实际偏移量。
var rawPubDate = results[i].get('pubDate');
var fixedPubDate = rawPubDate.replace('PDT', '-07:00');
fixedPubDate = fixedPubDate.replace('PST', '-08:00');
// add any others you need
var convertedPubDate = moment(fixedPubDate, 'YY MM DD HH mm ss Z').toDate();
console.log('pubDate: ' + rawPubDate
+ ' -> ' + fixedPubDate
+ ' -> ' + convertedPubDate);
// ... etc ...